Skip to content

Commit 2bcb547

Browse files
committed
Merge pull request #176 from tamakisquare/modal-controlleras-option
feat(modal): Add support for 'controllerAs' option
2 parents 80fd5ea + 1a41f22 commit 2bcb547

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/modal/docs/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The `$modal` service has only one method: `open(options)` where available option
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`
99
* `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`
10+
* `controllerAs` - an alternative to the aforementioned "controller-as" syntax. This is intended for the case of when the `controller` option is assigned an anonymous or local function and thus the "controller-as" syntax couldn't be applied.
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

src/modal/modal.js

+3
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ angular.module('mm.foundation.modal', ['mm.foundation.transition'])
362362
});
363363

364364
ctrlInstance = $controller(modalOptions.controller, ctrlLocals);
365+
if (modalOptions.controllerAs) {
366+
modalScope[modalOptions.controllerAs] = ctrlInstance;
367+
}
365368
}
366369

367370
$modalStack.open(modalInstance, {

src/modal/test/modal.spec.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe('$modal', function () {
321321
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
322322
});
323323

324-
it('should accept controllerAs alias', function () {
324+
it('should accept controller-as syntax in `controller` option', function () {
325325
$controllerProvider.register('TestCtrl', function($modalInstance) {
326326
this.fromCtrl = 'Content from ctrl';
327327
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
@@ -331,6 +331,18 @@ describe('$modal', function () {
331331
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
332332
});
333333

334+
it('should accept `controllerAs` option', function () {
335+
var modal = open({
336+
template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>',
337+
controller: function($modalInstance) {
338+
this.fromCtrl = 'Content from ctrl';
339+
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
340+
},
341+
controllerAs: 'test'
342+
});
343+
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
344+
});
345+
334346
});
335347

336348
describe('resolve', function () {

0 commit comments

Comments
 (0)