Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 1dbad8a

Browse files
onumossnwesleycho
authored andcommitted
fix(modal): change to compile before appending
- Change to compile the modal window before inserting into the DOM Closes #5224 Fixes #5183
1 parent 78ba137 commit 1dbad8a

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

src/modal/modal.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,8 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
477477
angularDomEl.attr('modal-animation', 'true');
478478
}
479479

480-
$animate.enter(angularDomEl, appendToElement)
480+
$animate.enter($compile(angularDomEl)(modal.scope), appendToElement)
481481
.then(function() {
482-
$compile(angularDomEl)(modal.scope);
483482
$animate.addClass(appendToElement, modalBodyClass);
484483
});
485484

src/modal/test/modal.spec.js

+52-32
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,16 @@ describe('$uibModal', function () {
213213
element.trigger(e);
214214
}
215215

216-
function open(modalOptions, noFlush) {
216+
function open(modalOptions, noFlush, noDigest) {
217217
var modal = $uibModal.open(modalOptions);
218-
$rootScope.$digest();
219-
if (!noFlush) {
220-
$animate.flush();
218+
219+
if (!noDigest) {
220+
$rootScope.$digest();
221+
if (!noFlush) {
222+
$animate.flush();
223+
}
221224
}
225+
222226
return modal;
223227
}
224228

@@ -261,6 +265,46 @@ describe('$uibModal', function () {
261265
expect($document).not.toHaveBackdrop();
262266
});
263267

268+
it('should compile modal before inserting into DOM', function() {
269+
var topModal;
270+
var modalInstance = {
271+
result: $q.defer(),
272+
opened: $q.defer(),
273+
closed: $q.defer(),
274+
rendered: $q.defer(),
275+
close: function (result) {
276+
return $uibModalStack.close(modalInstance, result);
277+
},
278+
dismiss: function (reason) {
279+
return $uibModalStack.dismiss(modalInstance, reason);
280+
}
281+
};
282+
var expectedText = 'test';
283+
284+
$uibModalStack.open(modalInstance, {
285+
appendTo: angular.element(document.body),
286+
scope: $rootScope.$new(),
287+
deferred: modalInstance.result,
288+
renderDeferred: modalInstance.rendered,
289+
closedDeferred: modalInstance.closed,
290+
content: '<div id="test">{{\'' + expectedText + '\'}}</div>'
291+
});
292+
293+
topModal = $uibModalStack.getTop();
294+
295+
expect(topModal.value.modalDomEl.find('#test').length).toEqual(0);
296+
expect(angular.element('#test').length).toEqual(0);
297+
298+
$rootScope.$digest();
299+
300+
expect(topModal.value.modalDomEl.find('#test').text()).toEqual(expectedText);
301+
expect(angular.element('#test').text()).toEqual(expectedText);
302+
303+
$animate.flush();
304+
305+
close(modalInstance, 'closing in test', true);
306+
});
307+
264308
it('should not throw an exception on a second dismiss', function() {
265309
var modal = open({template: '<div>Content</div>'});
266310

@@ -364,7 +408,6 @@ describe('$uibModal', function () {
364408
expect(document.activeElement.tagName).toBe('A');
365409

366410
var modal = open({template: '<div>Content<button>inside modal</button></div>'});
367-
$animate.flush();
368411
$rootScope.$digest();
369412
expect(document.activeElement.tagName).toBe('DIV');
370413
expect($document).toHaveModalsOpen(1);
@@ -389,7 +432,6 @@ describe('$uibModal', function () {
389432
expect(document.activeElement.tagName).toBe('A');
390433

391434
var modal = open({template: '<div>Content</div>'});
392-
$animate.flush();
393435
$rootScope.$digest();
394436
expect(document.activeElement.tagName).toBe('DIV');
395437
expect($document).toHaveModalsOpen(1);
@@ -468,7 +510,6 @@ describe('$uibModal', function () {
468510
it('should focus on the element that has autofocus attribute when the modal is open/reopen and the animations have finished', function() {
469511
function openAndCloseModalWithAutofocusElement() {
470512
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus></div>'});
471-
$animate.flush();
472513
$rootScope.$digest();
473514
expect(angular.element('#auto-focus-element')).toHaveFocus();
474515

@@ -485,7 +526,6 @@ describe('$uibModal', function () {
485526
function openAndCloseModalWithAutofocusElement() {
486527

487528
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus><input type="text" id="pre-focus-element" focus-me></div>'});
488-
$animate.flush();
489529
$rootScope.$digest();
490530
expect(angular.element('#auto-focus-element')).not.toHaveFocus();
491531
expect(angular.element('#pre-focus-element')).toHaveFocus();
@@ -501,11 +541,11 @@ describe('$uibModal', function () {
501541

502542
it('should wait until the in animation is finished before attempting to focus the modal or autofocus element', function() {
503543
function openAndCloseModalWithAutofocusElement() {
504-
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus></div>'});
544+
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus></div>'}, true, true);
505545
expect(angular.element('#auto-focus-element')).not.toHaveFocus();
506546

507-
$animate.flush();
508547
$rootScope.$digest();
548+
$animate.flush();
509549

510550
expect(angular.element('#auto-focus-element')).toHaveFocus();
511551

@@ -521,11 +561,11 @@ describe('$uibModal', function () {
521561
element.focus();
522562
expect(document.activeElement.tagName).toBe('A');
523563

524-
var modal = open({template: '<div><input type="text"></div>'});
564+
var modal = open({template: '<div><input type="text"></div>'}, true, true);
525565
expect(document.activeElement.tagName).toBe('A');
526566

527-
$animate.flush();
528567
$rootScope.$digest();
568+
$animate.flush();
529569

530570
expect(document.activeElement.tagName).toBe('DIV');
531571

@@ -788,7 +828,6 @@ describe('$uibModal', function () {
788828
expect($document).toHaveModalsOpen(0);
789829

790830
$timeout.flush();
791-
$animate.flush();
792831
expect($document).toHaveModalOpenWithContent('Promise', 'div');
793832
});
794833

@@ -886,14 +925,12 @@ describe('$uibModal', function () {
886925

887926
it('should contain backdrop in classes on each modal opening', function() {
888927
var modal = open({ template: '<div>With backdrop</div>' });
889-
$animate.flush();
890928
var backdropEl = $document.find('body > div.modal-backdrop');
891929
expect(backdropEl).toHaveClass('in');
892930

893931
dismiss(modal);
894932

895933
modal = open({ template: '<div>With backdrop</div>' });
896-
$animate.flush();
897934
backdropEl = $document.find('body > div.modal-backdrop');
898935
expect(backdropEl).toHaveClass('in');
899936

@@ -1031,7 +1068,6 @@ describe('$uibModal', function () {
10311068
angular.element(document.body).append(element);
10321069

10331070
open({template: '<div child-directive>{{text}}</div>', appendTo: element});
1034-
$animate.flush();
10351071
expect($document.find('[child-directive]').text()).toBe('foo');
10361072

10371073
element.remove();
@@ -1050,8 +1086,6 @@ describe('$uibModal', function () {
10501086
template: '<div>dummy modal</div>'
10511087
});
10521088

1053-
$animate.flush();
1054-
10551089
expect(body).toHaveClass('modal-open');
10561090
});
10571091

@@ -1061,8 +1095,6 @@ describe('$uibModal', function () {
10611095
openedClass: 'foo'
10621096
});
10631097

1064-
$animate.flush();
1065-
10661098
expect(body).toHaveClass('foo');
10671099
expect(body).not.toHaveClass('modal-open');
10681100
});
@@ -1073,8 +1105,6 @@ describe('$uibModal', function () {
10731105
openedClass: 'foo'
10741106
});
10751107

1076-
$animate.flush();
1077-
10781108
expect(body).toHaveClass('foo');
10791109

10801110
close(modal);
@@ -1088,8 +1118,6 @@ describe('$uibModal', function () {
10881118
openedClass: 'foo'
10891119
});
10901120

1091-
$animate.flush();
1092-
10931121
expect(body).toHaveClass('foo');
10941122
expect(body).not.toHaveClass('modal-open');
10951123

@@ -1098,8 +1126,6 @@ describe('$uibModal', function () {
10981126
openedClass: 'bar'
10991127
});
11001128

1101-
$animate.flush();
1102-
11031129
expect(body).toHaveClass('foo');
11041130
expect(body).toHaveClass('bar');
11051131
expect(body).not.toHaveClass('modal-open');
@@ -1109,8 +1135,6 @@ describe('$uibModal', function () {
11091135
openedClass: 'foo'
11101136
});
11111137

1112-
$animate.flush();
1113-
11141138
expect(body).toHaveClass('foo');
11151139
expect(body).toHaveClass('bar');
11161140
expect(body).not.toHaveClass('modal-open');
@@ -1232,11 +1256,9 @@ describe('$uibModal', function () {
12321256
expect(body).not.toHaveClass('modal-open');
12331257

12341258
var modal1 = open({template: '<div>Content1</div>'});
1235-
$animate.flush();
12361259
expect(body).toHaveClass('modal-open');
12371260

12381261
var modal2 = open({template: '<div>Content1</div>'});
1239-
$animate.flush();
12401262
expect(body).toHaveClass('modal-open');
12411263

12421264
dismiss(modal1);
@@ -1254,14 +1276,12 @@ describe('$uibModal', function () {
12541276
expect(document.activeElement.tagName).toBe('A');
12551277

12561278
var modal1 = open({template: '<div>Modal1<button id="focus">inside modal1</button></div>'});
1257-
$animate.flush();
12581279
$rootScope.$digest();
12591280
document.getElementById('focus').focus();
12601281
expect(document.activeElement.tagName).toBe('BUTTON');
12611282
expect($document).toHaveModalsOpen(1);
12621283

12631284
var modal2 = open({template: '<div>Modal2</div>'});
1264-
$animate.flush();
12651285
$rootScope.$digest();
12661286
expect(document.activeElement.tagName).toBe('DIV');
12671287
expect($document).toHaveModalsOpen(2);

template/modal/window.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
uib-modal-animation-class="fade"
33
modal-in-class="in"
44
ng-style="{'z-index': 1050 + index*10, display: 'block'}">
5-
<div class="modal-dialog" ng-class="size ? 'modal-' + size : ''"><div class="modal-content" uib-modal-transclude></div></div>
5+
<div class="modal-dialog {{size ? 'modal-' + size : ''}}"><div class="modal-content" uib-modal-transclude></div></div>
66
</div>

0 commit comments

Comments
 (0)