@@ -75,7 +75,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
75
75
} ;
76
76
} ] )
77
77
78
- . directive ( 'modalWindow' , [ '$modalStack' , '$timeout ' , function ( $modalStack , $timeout ) {
78
+ . directive ( 'modalWindow' , [ '$modalStack' , '$q ' , function ( $modalStack , $q ) {
79
79
return {
80
80
restrict : 'EA' ,
81
81
scope : {
@@ -91,7 +91,31 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
91
91
element . addClass ( attrs . windowClass || '' ) ;
92
92
scope . size = attrs . size ;
93
93
94
- $timeout ( function ( ) {
94
+ scope . close = function ( evt ) {
95
+ var modal = $modalStack . getTop ( ) ;
96
+ if ( modal && modal . value . backdrop && modal . value . backdrop != 'static' && ( evt . target === evt . currentTarget ) ) {
97
+ evt . preventDefault ( ) ;
98
+ evt . stopPropagation ( ) ;
99
+ $modalStack . dismiss ( modal . key , 'backdrop click' ) ;
100
+ }
101
+ } ;
102
+
103
+ // This property is only added to the scope for the purpose of detecting when this directive is rendered.
104
+ // We can detect that by using this property in the template associated with this directive and then use
105
+ // {@link Attribute#$observe } on it. For more details please see {@link TableColumnResize }.
106
+ scope . $isRendered = true ;
107
+
108
+ // Deferred object that will be resolved when this modal is render.
109
+ var modalRenderDeferObj = $q . defer ( ) ;
110
+ // Observe function will be called on next digest cycle after compilation, ensuring that the DOM is ready.
111
+ // In order to use this way of finding whether DOM is ready, we need to observe a scope property used in modal's template.
112
+ attrs . $observe ( 'modalRender' , function ( value ) {
113
+ if ( value == 'true' ) {
114
+ modalRenderDeferObj . resolve ( ) ;
115
+ }
116
+ } ) ;
117
+
118
+ modalRenderDeferObj . promise . then ( function ( ) {
95
119
// trigger CSS transitions
96
120
scope . animate = true ;
97
121
@@ -106,16 +130,13 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
106
130
if ( ! element [ 0 ] . querySelectorAll ( '[autofocus]' ) . length ) {
107
131
element [ 0 ] . focus ( ) ;
108
132
}
109
- } ) ;
110
133
111
- scope . close = function ( evt ) {
134
+ // Notify { @link $modalStack } that modal is rendered.
112
135
var modal = $modalStack . getTop ( ) ;
113
- if ( modal && modal . value . backdrop && modal . value . backdrop != 'static' && ( evt . target === evt . currentTarget ) ) {
114
- evt . preventDefault ( ) ;
115
- evt . stopPropagation ( ) ;
116
- $modalStack . dismiss ( modal . key , 'backdrop click' ) ;
136
+ if ( modal ) {
137
+ $modalStack . modalRendered ( modal . key ) ;
117
138
}
118
- } ;
139
+ } ) ;
119
140
}
120
141
} ;
121
142
} ] )
@@ -236,6 +257,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
236
257
237
258
openedWindows . add ( modalInstance , {
238
259
deferred : modal . deferred ,
260
+ renderDeferred : modal . renderDeferred ,
239
261
modalScope : modal . scope ,
240
262
backdrop : modal . backdrop ,
241
263
keyboard : modal . keyboard
@@ -296,6 +318,13 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
296
318
return openedWindows . top ( ) ;
297
319
} ;
298
320
321
+ $modalStack . modalRendered = function ( modalInstance ) {
322
+ var modalWindow = openedWindows . get ( modalInstance ) ;
323
+ if ( modalWindow ) {
324
+ modalWindow . value . renderDeferred . resolve ( ) ;
325
+ }
326
+ } ;
327
+
299
328
return $modalStack ;
300
329
} ] )
301
330
@@ -333,11 +362,13 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
333
362
334
363
var modalResultDeferred = $q . defer ( ) ;
335
364
var modalOpenedDeferred = $q . defer ( ) ;
365
+ var modalRenderDeferred = $q . defer ( ) ;
336
366
337
367
//prepare an instance of a modal to be injected into controllers and returned to a caller
338
368
var modalInstance = {
339
369
result : modalResultDeferred . promise ,
340
370
opened : modalOpenedDeferred . promise ,
371
+ rendered : modalRenderDeferred . promise ,
341
372
close : function ( result ) {
342
373
$modalStack . close ( modalInstance , result ) ;
343
374
} ,
@@ -385,6 +416,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
385
416
$modalStack . open ( modalInstance , {
386
417
scope : modalScope ,
387
418
deferred : modalResultDeferred ,
419
+ renderDeferred : modalRenderDeferred ,
388
420
content : tplAndVars [ 0 ] ,
389
421
backdrop : modalOptions . backdrop ,
390
422
keyboard : modalOptions . keyboard ,
0 commit comments