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

Commit 8d7c2a2

Browse files
feat(modal): support alternative controllerAs syntax
Fixes #2242
1 parent b519b63 commit 8d7c2a2

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/modal/docs/readme.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ The `$modal` service has only one method: `open(options)` where available option
66
* `templateUrl` - a path to a template representing modal's content
77
* `template` - inline template representing the modal's content
88
* `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`
9-
* `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`
9+
* `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`
10+
* `controllerAs` - an alternative to the controller-as syntax, matching the API of directive definitions. Requires the `controller` option to be provided as well
1011
* `resolve` - members that will be resolved and passed to the controller as locals; it is equivalent of the `resolve` property for AngularJS routes
1112
* `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.
1213
* `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
2728
* `$close(result)`
2829
* `$dismiss(reason)`
2930

30-
Those methods make it easy to close a modal window without a need to create a dedicated controller
31+
Those methods make it easy to close a modal window without a need to create a dedicated controller.

src/modal/modal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
365365
ctrlLocals[key] = tplAndVars[resolveIter++];
366366
});
367367

368-
$controller(modalOptions.controller, ctrlLocals);
368+
$controller(modalOptions.controllerAs ? modalOptions.controller + ' as ' + modalOptions.controllerAs : modalOptions.controller, ctrlLocals);
369369
}
370370

371371
$modalStack.open(modalInstance, {

src/modal/test/modal.spec.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('$modal', function () {
2-
var $rootScope, $document, $compile, $templateCache, $timeout, $q;
2+
var $controllerProvider, $rootScope, $document, $compile, $templateCache, $timeout, $q;
33
var $modal, $modalProvider;
44

55
var triggerKeyDown = function (element, keyCode) {
@@ -290,7 +290,7 @@ describe('$modal', function () {
290290
$scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
291291
};
292292

293-
var modal = open({template: '<div>{{fromCtrl}} {{isModalInstance}}</div>', controller: TestCtrl});
293+
open({template: '<div>{{fromCtrl}} {{isModalInstance}}</div>', controller: TestCtrl});
294294
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
295295
});
296296

@@ -300,10 +300,19 @@ describe('$modal', function () {
300300
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
301301
});
302302

303-
var modal = open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl as test'});
303+
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl as test'});
304304
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
305305
});
306306

307+
it('should respect the controllerAs property as an alternative for the controller-as syntax', function () {
308+
$controllerProvider.register('TestCtrl', function($modalInstance) {
309+
this.fromCtrl = 'Content from ctrl';
310+
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
311+
});
312+
313+
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl', controllerAs: 'test'});
314+
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
315+
});
307316
});
308317

309318
describe('resolve', function () {

0 commit comments

Comments
 (0)