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

Commit

Permalink
feat(modal): support alternative controllerAs syntax
Browse files Browse the repository at this point in the history
Fixes #2242
  • Loading branch information
pkozlowski-opensource committed May 22, 2014
1 parent b519b63 commit 8d7c2a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/modal/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
2 changes: 1 addition & 1 deletion src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
15 changes: 12 additions & 3 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('$modal', function () {
$scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
};

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

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

var modal = open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl as test'});
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', 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: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl', controllerAs: 'test'});
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
});
});

describe('resolve', function () {
Expand Down

1 comment on commit 8d7c2a2

@techniq
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just what I was looking for. Any rough idea when this might make it into a release? For now I'll just pull the bower package from this commit.

Please sign in to comment.