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

Commit

Permalink
fix(modal): fix bindToController props
Browse files Browse the repository at this point in the history
- Bind provided $scope props to controller before instantiation
- Make $onInit available regardless of controllerAs usage

Closes #5569
  • Loading branch information
kuitos authored and wesleycho committed Mar 3, 2016
1 parent 9d74d6c commit 5e43870
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}
});

var ctrlInstance, ctrlLocals = {};
var ctrlInstance, ctrlInstantiate, ctrlLocals = {};

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

ctrlInstance = $controller(modalOptions.controller, ctrlLocals);
// the third param will make the controller instantiate later,private api
// @see https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L126
ctrlInstantiate = $controller(modalOptions.controller, ctrlLocals, true);
if (modalOptions.controllerAs) {
ctrlInstance = ctrlInstantiate.instance;

if (modalOptions.bindToController) {
ctrlInstance.$close = modalScope.$close;
ctrlInstance.$dismiss = modalScope.$dismiss;
angular.extend(ctrlInstance, providedScope);
if (angular.isFunction(ctrlInstance.$onInit)) {
ctrlInstance.$onInit();
}
}

ctrlInstance = ctrlInstantiate();

modalScope[modalOptions.controllerAs] = ctrlInstance;
} else {
ctrlInstance = ctrlInstantiate();
}

if (angular.isFunction(ctrlInstance.$onInit)) {
ctrlInstance.$onInit();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ describe('$uibModal', function() {
open({
template: '<div>{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}</div>',
controller: function($uibModalInstance) {
expect(this.foo).toEqual($scope.foo);
this.fromCtrl = 'Content from ctrl';
this.closeDismissPresent = function() {
return angular.isFunction(this.$close) && angular.isFunction(this.$dismiss);
Expand Down

0 comments on commit 5e43870

Please sign in to comment.