Skip to content

Commit a665d1d

Browse files
urishmhartington
authored andcommitted
feat(NavView): support ControllerAs syntax for ion-nav-views
Closes #3058. Closes #2499. Cloese #3651 This adds support for setting up controllers like this .state('app', { url: '/app', abstract: true, templateUrl: 'templates/menu.html', controller: 'AppCtrl', controllerAs: 'main' })
1 parent e5e410a commit a665d1d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Diff for: js/angular/controller/navViewController.js

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
267267
if (viewLocals && viewLocals.$$controller) {
268268
viewLocals.$scope = viewScope;
269269
var controller = $controller(viewLocals.$$controller, viewLocals);
270+
if (viewLocals.$$controllerAs) {
271+
viewScope[viewLocals.$$controllerAs] = controller;
272+
}
270273
$element.children().data('$ngControllerController', controller);
271274
}
272275

Diff for: test/unit/angular/controller/navViewController.unit.js

+21
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,26 @@ describe('$ionicNavView controller', function() {
8181
expect(navBarCtrl.title()).toBe('My title');
8282
}));
8383

84+
it('should support controllerAs syntax', inject(function($rootScope, $compile) {
85+
var containerEle = angular.element('<div>');
86+
var navBarEle = angular.element('<ion-nav-bar>');
87+
var navViewEle = angular.element('<ion-nav-view>');
88+
var innerElement = angular.element('<content>');
89+
90+
containerEle.append(navBarEle);
91+
navBarEle.append(navViewEle);
92+
93+
$compile(containerEle)($rootScope);
94+
95+
var navViewCtrl = navViewEle.data('$ionNavViewController');
96+
var navViewScope = navViewEle.scope();
97+
98+
var ViewCtrlConstructor = jasmine.createSpy('ViewCtrlConstructor');
99+
navViewCtrl.appendViewElement(innerElement, {$$controller: ViewCtrlConstructor, $$controllerAs: 'vm'});
100+
101+
var innerScope = innerElement.scope();
102+
expect(ViewCtrlConstructor).toHaveBeenCalled();
103+
expect(innerScope.vm instanceof ViewCtrlConstructor).toBeTruthy();
104+
}));
84105

85106
});

0 commit comments

Comments
 (0)