From ae18a0953ace3089b61bb5fbbf5bbb663cdfcb0b Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Thu, 10 Dec 2015 09:15:11 -0800 Subject: [PATCH] fix(modal): fix copying from $scope - 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 --- src/modal/modal.js | 7 +++++-- src/modal/test/modal.spec.js | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index ab6c38e3b6..c846cf9bf0 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -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; @@ -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; diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index d9192aa4aa..2220544d0d 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -663,11 +663,21 @@ describe('$uibModal', function () { }); it('should allow usage of bindToController', function() { - open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', 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: '
{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}
', + 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'); }); });