From 1ee7334a73efeccc9b95340e315cdfd59944762d Mon Sep 17 00:00:00 2001 From: Sean Clark Hess Date: Thu, 23 Jan 2014 11:11:33 -0700 Subject: [PATCH] feat(uiView): add controllerAs config with function need to support function pointers so we can pass them in --- src/viewDirective.js | 3 +++ test/viewDirectiveSpec.js | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/viewDirective.js b/src/viewDirective.js index 7c22145c2..6e0813ad0 100644 --- a/src/viewDirective.js +++ b/src/viewDirective.js @@ -157,6 +157,9 @@ function $ViewDirective( $state, $compile, $controller, $injector, $ui if (locals.$$controller) { locals.$scope = currentScope; var controller = $controller(locals.$$controller, locals); + if ($state.$current.controllerAs) { + currentScope[$state.$current.controllerAs] = controller; + } currentEl.children().data('$ngControllerController', controller); } diff --git a/test/viewDirectiveSpec.js b/test/viewDirectiveSpec.js index 02cb65e82..8e1cf17a7 100644 --- a/test/viewDirectiveSpec.js +++ b/test/viewDirectiveSpec.js @@ -77,6 +77,12 @@ describe('uiView', function () { }, jState = { template: 'jState' + }, + kState = { + controller: function() { + this.someProperty = "value" + }, + controllerAs: "vm", }; beforeEach(module(function ($stateProvider) { @@ -90,7 +96,8 @@ describe('uiView', function () { .state('g', gState) .state('g.h', hState) .state('i', iState) - .state('j', jState); + .state('j', jState) + .state('k', kState) })); beforeEach(inject(function ($rootScope, _$compile_) { @@ -307,6 +314,15 @@ describe('uiView', function () { expect($uiViewScroll).toHaveBeenCalledWith(target); })); + + it('should instantiate a controller with controllerAs', inject(function($state, $q) { + elem.append($compile('
{{vm.someProperty}}
')(scope)); + $state.transitionTo(kState); + $q.flush(); + var innerScope = scope.$$childHead + expect(innerScope.vm).not.toBeUndefined() + expect(innerScope.vm.someProperty).toBe("value") + })) }); }); \ No newline at end of file