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
Browse files Browse the repository at this point in the history
- Fixes issue where `$scope` provided does not have properties present
  on controller instance due to $new resulting in the property on the
prototype of the $scope copied from, which causes it to not be
enumerable

Closes #5048
Fixes #5039
  • Loading branch information
wesleycho committed Dec 10, 2015
1 parent 2460e42 commit b8969d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
samePromise = promiseChain = $q.all([promiseChain])
.then(resolveWithTemplate, resolveWithTemplate)
.then(function resolveSuccess(tplAndVars) {
var providedScope = modalOptions.scope || $rootScope;

var modalScope = (modalOptions.scope || $rootScope).$new();
var modalScope = providedScope.$new();
modalScope.$close = modalInstance.close;
modalScope.$dismiss = modalInstance.dismiss;

Expand All @@ -641,7 +642,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
ctrlInstance = $controller(modalOptions.controller, ctrlLocals);
if (modalOptions.controllerAs) {
if (modalOptions.bindToController) {
angular.extend(ctrlInstance, modalScope);
ctrlInstance.$close = modalScope.$close;
ctrlInstance.$dismiss = modalScope.$dismiss;
angular.extend(ctrlInstance, providedScope);
}

modalScope[modalOptions.controllerAs] = ctrlInstance;
Expand Down
20 changes: 15 additions & 5 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,21 @@ describe('$uibModal', function () {
});

it('should allow usage of bindToController', function() {
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($uibModalInstance) {
this.fromCtrl = 'Content from ctrl';
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
}, controllerAs: 'test', bindToController: true});
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
var $scope = $rootScope.$new(true);
$scope.foo = 'bar';
open({
template: '<div>{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}</div>',
controller: function($uibModalInstance) {
this.fromCtrl = 'Content from ctrl';
this.closeDismissPresent = function() {
return angular.isFunction(this.$close) && angular.isFunction(this.$dismiss);
};
},
controllerAs: 'test',
bindToController: true,
scope: $scope
});
expect($document).toHaveModalOpenWithContent('Content from ctrl true bar', 'div');
});
});

Expand Down

0 comments on commit b8969d1

Please sign in to comment.