Skip to content

Commit 165ac1d

Browse files
committed
Attaching Controllers on The Scope
1 parent 126306f commit 165ac1d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function $ControllerProvider() {
3131

3232
return function(ctrl, locals, later, identifier) {
3333
if (_.isString(ctrl)) {
34+
var match = ctrl.match(/^(\S+)(\s+as\s+(\w+))?/);
35+
ctrl = match[1];
36+
identifier = identifier || match[3];
3437
if (controllers.hasOwnProperty(ctrl)) {
3538
ctrl = controllers[ctrl];
3639
} else if (globals) {

test/directives/ng_controller_spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ describe('ngController', function() {
5656
});
5757
});
5858

59+
it('allows aliasing controller in expression', function() {
60+
var gotScope;
61+
function MyController($scope, $element, $attrs) {
62+
gotScope = $scope;
63+
}
64+
var injector = createInjector(['ng', function($controllerProvider) {
65+
$controllerProvider.register('MyController', MyController);
66+
}]);
67+
injector.invoke(function($compile, $rootScope) {
68+
var el = $('<div ng-controller="MyController as myCtrl"></div>');
69+
$compile(el)($rootScope);
70+
expect(gotScope.myCtrl).toBeDefined();
71+
expect(gotScope.myCtrl instanceof MyController).toBe(true);
72+
});
73+
});
74+
5975
});

0 commit comments

Comments
 (0)