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

fix(modal): fix bindToController bug #5569

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}
});

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

//controllers
if (modalOptions.controller) {
Expand All @@ -681,18 +681,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