From 8d7c2a263fcba3ba136ef7d97df59b11956c9453 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 22 May 2014 21:26:19 +0200 Subject: [PATCH] feat(modal): support alternative controllerAs syntax Fixes #2242 --- src/modal/docs/readme.md | 5 +++-- src/modal/modal.js | 2 +- src/modal/test/modal.spec.js | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/modal/docs/readme.md b/src/modal/docs/readme.md index 1c36320f00..89b115b109 100644 --- a/src/modal/docs/readme.md +++ b/src/modal/docs/readme.md @@ -6,7 +6,8 @@ The `$modal` service has only one method: `open(options)` where available option * `templateUrl` - a path to a template representing modal's content * `template` - inline template representing the modal's content * `scope` - a scope instance to be used for the modal's content (actually the `$modal` service is going to create a child scope of a provided scope). Defaults to `$rootScope` -* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax, and can be injected with `$modalInstance` +* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with `$modalInstance` +* `controllerAs` - an alternative to the controller-as syntax, matching the API of directive definitions. Requires the `controller` option to be provided as well * `resolve` - members that will be resolved and passed to the controller as locals; it is equivalent of the `resolve` property for AngularJS routes * `backdrop` - controls presence of a backdrop. Allowed values: true (default), false (no backdrop), `'static'` - backdrop is present but modal window is not closed when clicking outside of the modal window. * `keyboard` - indicates whether the dialog should be closable by hitting the ESC key, defaults to true @@ -27,4 +28,4 @@ In addition the scope associated with modal's content is augmented with 2 method * `$close(result)` * `$dismiss(reason)` -Those methods make it easy to close a modal window without a need to create a dedicated controller +Those methods make it easy to close a modal window without a need to create a dedicated controller. diff --git a/src/modal/modal.js b/src/modal/modal.js index 043596484c..d17377b826 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -365,7 +365,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition']) ctrlLocals[key] = tplAndVars[resolveIter++]; }); - $controller(modalOptions.controller, ctrlLocals); + $controller(modalOptions.controllerAs ? modalOptions.controller + ' as ' + modalOptions.controllerAs : modalOptions.controller, ctrlLocals); } $modalStack.open(modalInstance, { diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 153b531d0a..3a9d33c1f4 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -1,5 +1,5 @@ describe('$modal', function () { - var $rootScope, $document, $compile, $templateCache, $timeout, $q; + var $controllerProvider, $rootScope, $document, $compile, $templateCache, $timeout, $q; var $modal, $modalProvider; var triggerKeyDown = function (element, keyCode) { @@ -290,7 +290,7 @@ describe('$modal', function () { $scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); }; - var modal = open({template: '
{{fromCtrl}} {{isModalInstance}}
', controller: TestCtrl}); + open({template: '
{{fromCtrl}} {{isModalInstance}}
', controller: TestCtrl}); expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div'); }); @@ -300,10 +300,19 @@ describe('$modal', function () { this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); }); - var modal = open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: 'TestCtrl as test'}); + open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: 'TestCtrl as test'}); expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div'); }); + it('should respect the controllerAs property as an alternative for the controller-as syntax', function () { + $controllerProvider.register('TestCtrl', function($modalInstance) { + this.fromCtrl = 'Content from ctrl'; + this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + }); + + open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: 'TestCtrl', controllerAs: 'test'}); + expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div'); + }); }); describe('resolve', function () {