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

Commit b8969d1

Browse files
committed
fix(modal): fix bindToController
- 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
1 parent 2460e42 commit b8969d1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/modal/modal.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
616616
samePromise = promiseChain = $q.all([promiseChain])
617617
.then(resolveWithTemplate, resolveWithTemplate)
618618
.then(function resolveSuccess(tplAndVars) {
619+
var providedScope = modalOptions.scope || $rootScope;
619620

620-
var modalScope = (modalOptions.scope || $rootScope).$new();
621+
var modalScope = providedScope.$new();
621622
modalScope.$close = modalInstance.close;
622623
modalScope.$dismiss = modalInstance.dismiss;
623624

@@ -641,7 +642,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
641642
ctrlInstance = $controller(modalOptions.controller, ctrlLocals);
642643
if (modalOptions.controllerAs) {
643644
if (modalOptions.bindToController) {
644-
angular.extend(ctrlInstance, modalScope);
645+
ctrlInstance.$close = modalScope.$close;
646+
ctrlInstance.$dismiss = modalScope.$dismiss;
647+
angular.extend(ctrlInstance, providedScope);
645648
}
646649

647650
modalScope[modalOptions.controllerAs] = ctrlInstance;

src/modal/test/modal.spec.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,21 @@ describe('$uibModal', function () {
663663
});
664664

665665
it('should allow usage of bindToController', function() {
666-
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($uibModalInstance) {
667-
this.fromCtrl = 'Content from ctrl';
668-
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
669-
}, controllerAs: 'test', bindToController: true});
670-
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
666+
var $scope = $rootScope.$new(true);
667+
$scope.foo = 'bar';
668+
open({
669+
template: '<div>{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}</div>',
670+
controller: function($uibModalInstance) {
671+
this.fromCtrl = 'Content from ctrl';
672+
this.closeDismissPresent = function() {
673+
return angular.isFunction(this.$close) && angular.isFunction(this.$dismiss);
674+
};
675+
},
676+
controllerAs: 'test',
677+
bindToController: true,
678+
scope: $scope
679+
});
680+
expect($document).toHaveModalOpenWithContent('Content from ctrl true bar', 'div');
671681
});
672682
});
673683

0 commit comments

Comments
 (0)