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 2 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
20 changes: 15 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,28 @@ 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) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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();

if (modalOptions.bindToController && angular.isFunction(ctrlInstance.$onInit)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is bindToController being required here for $onInit usage?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://docs.angularjs.org/guide/component, in the Intercomponent Communication section, $onInit does NOT require bindToController being present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes u are right,I had checked the source code of angular.js,it does not require controllerAs too.Pls review the latest commit.🙂

ctrlInstance.$onInit();
}

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

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