Skip to content

Commit

Permalink
add controllerAs config property
Browse files Browse the repository at this point in the history
  • Loading branch information
artch committed Aug 20, 2013
1 parent 89d94f1 commit ada1eda
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ Adds new segment at current pointer level.
- `template` provides HTML for the given segment view;
- `templateUrl` is a template which should be fetched from the network via this URL;
- `controller` is attached to the given segment view when compiled and linked, this can be any controller definition AngularJS supports;
- `controllerAs` is a controller alias name, if present the controller will be published to scope under the
controllerAs name;
- `dependencies` is an array of route param names which are forcing the view to recreate when changed;
- `watcher` is a $watch-function for recreating the view when its returning value is changed;
- `resolve` is a hash of functions or injectable names which should be resolved prior to instantiating the template and the controller;
Expand Down
2 changes: 2 additions & 0 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
if (segment.params.controller) {
locals.$scope = currentScope;
controller = $controller(segment.params.controller, locals);
if(segment.params.controllerAs)
currentScope[segment.params.controllerAs] = controller;
currentElement.data('$ngControllerController', controller);
currentElement.children().data('$ngControllerController', controller);
}
Expand Down
2 changes: 1 addition & 1 deletion build/angular-route-segment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/view-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
if (segment.params.controller) {
locals.$scope = currentScope;
controller = $controller(segment.params.controller, locals);
if(segment.params.controllerAs)
currentScope[segment.params.controllerAs] = controller;
currentElement.data('$ngControllerController', controller);
currentElement.children().data('$ngControllerController', controller);
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/view-segment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ describe('view-segment', function() {
expect(elm).toHaveClass('container');
expect(elm.find('> div > h4').text()).toMatch(/Section 1/);
})

it('should work with controllerAs syntax', function() {
$routeSegmentProvider.when('/3', 'section3');
$routeSegmentProvider.segment('section3', {
template: '<div></div>',
controller: function($scope) {},
controllerAs: 'ctrl'
})

scope = $rootScope.$new();
elm = angular.element('<div class="container" app:view-segment="0"></div>');
$compile(elm)(scope);
scope.$digest();

$location.path('/3');
$rootScope.$digest();

expect(elm.find('> div').scope().ctrl).toBeDefined();
expect(elm.find('> div').scope().ctrl).toBe(elm.find('> div').controller());
})


});
Expand Down

0 comments on commit ada1eda

Please sign in to comment.