Skip to content

Commit

Permalink
feat(NavView): support ControllerAs syntax for ion-nav-views
Browse files Browse the repository at this point in the history
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'
})
  • Loading branch information
urish authored and mhartington committed Aug 27, 2015
1 parent e5e410a commit a665d1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions js/angular/controller/navViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
if (viewLocals && viewLocals.$$controller) {
viewLocals.$scope = viewScope;
var controller = $controller(viewLocals.$$controller, viewLocals);
if (viewLocals.$$controllerAs) {
viewScope[viewLocals.$$controllerAs] = controller;
}
$element.children().data('$ngControllerController', controller);
}

Expand Down
21 changes: 21 additions & 0 deletions test/unit/angular/controller/navViewController.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,26 @@ describe('$ionicNavView controller', function() {
expect(navBarCtrl.title()).toBe('My title');
}));

it('should support controllerAs syntax', inject(function($rootScope, $compile) {
var containerEle = angular.element('<div>');
var navBarEle = angular.element('<ion-nav-bar>');
var navViewEle = angular.element('<ion-nav-view>');
var innerElement = angular.element('<content>');

containerEle.append(navBarEle);
navBarEle.append(navViewEle);

$compile(containerEle)($rootScope);

var navViewCtrl = navViewEle.data('$ionNavViewController');
var navViewScope = navViewEle.scope();

var ViewCtrlConstructor = jasmine.createSpy('ViewCtrlConstructor');
navViewCtrl.appendViewElement(innerElement, {$$controller: ViewCtrlConstructor, $$controllerAs: 'vm'});

var innerScope = innerElement.scope();
expect(ViewCtrlConstructor).toHaveBeenCalled();
expect(innerScope.vm instanceof ViewCtrlConstructor).toBeTruthy();
}));

});

0 comments on commit a665d1d

Please sign in to comment.