Skip to content

Commit 1ee7334

Browse files
committed
feat(uiView): add controllerAs config with function
need to support function pointers so we can pass them in
1 parent cf34271 commit 1ee7334

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/viewDirective.js

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ function $ViewDirective( $state, $compile, $controller, $injector, $ui
157157
if (locals.$$controller) {
158158
locals.$scope = currentScope;
159159
var controller = $controller(locals.$$controller, locals);
160+
if ($state.$current.controllerAs) {
161+
currentScope[$state.$current.controllerAs] = controller;
162+
}
160163
currentEl.children().data('$ngControllerController', controller);
161164
}
162165

test/viewDirectiveSpec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ describe('uiView', function () {
7777
},
7878
jState = {
7979
template: '<span ng-class="test">jState</span>'
80+
},
81+
kState = {
82+
controller: function() {
83+
this.someProperty = "value"
84+
},
85+
controllerAs: "vm",
8086
};
8187

8288
beforeEach(module(function ($stateProvider) {
@@ -90,7 +96,8 @@ describe('uiView', function () {
9096
.state('g', gState)
9197
.state('g.h', hState)
9298
.state('i', iState)
93-
.state('j', jState);
99+
.state('j', jState)
100+
.state('k', kState)
94101
}));
95102

96103
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -307,6 +314,15 @@ describe('uiView', function () {
307314

308315
expect($uiViewScroll).toHaveBeenCalledWith(target);
309316
}));
317+
318+
it('should instantiate a controller with controllerAs', inject(function($state, $q) {
319+
elem.append($compile('<div><ui-view>{{vm.someProperty}}</ui-view></div>')(scope));
320+
$state.transitionTo(kState);
321+
$q.flush();
322+
var innerScope = scope.$$childHead
323+
expect(innerScope.vm).not.toBeUndefined()
324+
expect(innerScope.vm.someProperty).toBe("value")
325+
}))
310326
});
311327

312328
});

0 commit comments

Comments
 (0)