Skip to content

Commit ada1eda

Browse files
committed
add controllerAs config property
1 parent 89d94f1 commit ada1eda

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ Adds new segment at current pointer level.
168168
- `template` provides HTML for the given segment view;
169169
- `templateUrl` is a template which should be fetched from the network via this URL;
170170
- `controller` is attached to the given segment view when compiled and linked, this can be any controller definition AngularJS supports;
171+
- `controllerAs` is a controller alias name, if present the controller will be published to scope under the
172+
controllerAs name;
171173
- `dependencies` is an array of route param names which are forcing the view to recreate when changed;
172174
- `watcher` is a $watch-function for recreating the view when its returning value is changed;
173175
- `resolve` is a hash of functions or injectable names which should be resolved prior to instantiating the template and the controller;

build/angular-route-segment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
498498
if (segment.params.controller) {
499499
locals.$scope = currentScope;
500500
controller = $controller(segment.params.controller, locals);
501+
if(segment.params.controllerAs)
502+
currentScope[segment.params.controllerAs] = controller;
501503
currentElement.data('$ngControllerController', controller);
502504
currentElement.children().data('$ngControllerController', controller);
503505
}

build/angular-route-segment.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/view-segment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
if (segment.params.controller) {
107107
locals.$scope = currentScope;
108108
controller = $controller(segment.params.controller, locals);
109+
if(segment.params.controllerAs)
110+
currentScope[segment.params.controllerAs] = controller;
109111
currentElement.data('$ngControllerController', controller);
110112
currentElement.children().data('$ngControllerController', controller);
111113
}

test/unit/view-segment.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ describe('view-segment', function() {
174174
expect(elm).toHaveClass('container');
175175
expect(elm.find('> div > h4').text()).toMatch(/Section 1/);
176176
})
177+
178+
it('should work with controllerAs syntax', function() {
179+
$routeSegmentProvider.when('/3', 'section3');
180+
$routeSegmentProvider.segment('section3', {
181+
template: '<div></div>',
182+
controller: function($scope) {},
183+
controllerAs: 'ctrl'
184+
})
185+
186+
scope = $rootScope.$new();
187+
elm = angular.element('<div class="container" app:view-segment="0"></div>');
188+
$compile(elm)(scope);
189+
scope.$digest();
190+
191+
$location.path('/3');
192+
$rootScope.$digest();
193+
194+
expect(elm.find('> div').scope().ctrl).toBeDefined();
195+
expect(elm.find('> div').scope().ctrl).toBe(elm.find('> div').controller());
196+
})
177197

178198

179199
});

0 commit comments

Comments
 (0)