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

Commit 5e43870

Browse files
kuitoswesleycho
authored andcommitted
fix(modal): fix bindToController props
- Bind provided $scope props to controller before instantiation - Make $onInit available regardless of controllerAs usage Closes #5569
1 parent 9d74d6c commit 5e43870

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/modal/modal.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
669669
}
670670
});
671671

672-
var ctrlInstance, ctrlLocals = {};
672+
var ctrlInstance, ctrlInstantiate, ctrlLocals = {};
673673

674674
//controllers
675675
if (modalOptions.controller) {
@@ -679,18 +679,27 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
679679
ctrlLocals[key] = value;
680680
});
681681

682-
ctrlInstance = $controller(modalOptions.controller, ctrlLocals);
682+
// the third param will make the controller instantiate later,private api
683+
// @see https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L126
684+
ctrlInstantiate = $controller(modalOptions.controller, ctrlLocals, true);
683685
if (modalOptions.controllerAs) {
686+
ctrlInstance = ctrlInstantiate.instance;
687+
684688
if (modalOptions.bindToController) {
685689
ctrlInstance.$close = modalScope.$close;
686690
ctrlInstance.$dismiss = modalScope.$dismiss;
687691
angular.extend(ctrlInstance, providedScope);
688-
if (angular.isFunction(ctrlInstance.$onInit)) {
689-
ctrlInstance.$onInit();
690-
}
691692
}
692693

694+
ctrlInstance = ctrlInstantiate();
695+
693696
modalScope[modalOptions.controllerAs] = ctrlInstance;
697+
} else {
698+
ctrlInstance = ctrlInstantiate();
699+
}
700+
701+
if (angular.isFunction(ctrlInstance.$onInit)) {
702+
ctrlInstance.$onInit();
694703
}
695704
}
696705

src/modal/test/modal.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ describe('$uibModal', function() {
815815
open({
816816
template: '<div>{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}</div>',
817817
controller: function($uibModalInstance) {
818+
expect(this.foo).toEqual($scope.foo);
818819
this.fromCtrl = 'Content from ctrl';
819820
this.closeDismissPresent = function() {
820821
return angular.isFunction(this.$close) && angular.isFunction(this.$dismiss);

0 commit comments

Comments
 (0)