Skip to content

Commit a1c3be2

Browse files
fix(angular1_router): rename $route service to $rootRouter
The singleton service that represents the top level router was called `$router` but this is confusing since there are actually lots of routers, which depend upon where you are in the DOM. This is similar to the situation with scopes. This commit clarifies this singleton by renaming it to `$rootRouter`. BREAKING CHANGE: The `$router` injectable service has been renamed to `$rootRouter`
1 parent edad8e3 commit a1c3be2

File tree

11 files changed

+141
-143
lines changed

11 files changed

+141
-143
lines changed

modules/angular1_router/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<script src="../../dist/angular_1_router.js"></script>
1313
<script>
1414
angular.module('myApp', ['ngComponentRouter'])
15-
.controller('MyCtrl', ['$router', function ($router) {
16-
console.log($router);
17-
$router.navigateByUrl('/')
15+
.controller('MyCtrl', ['$rootRouter', function ($rootRouter) {
16+
console.log($rootRouter);
17+
$rootRouter.navigateByUrl('/')
1818
.then(console.log.bind(console, 'resolve'), console.log.bind(console, 'reject'));
1919
}]);
2020
</script>

modules/angular1_router/src/module_template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular.module('ngComponentRouter').
44
// Because Angular 1 has no notion of a root component, we use an object with unique identity
55
// to represent this. Can be overloaded with a component name
66
value('$routerRootComponent', new Object()).
7-
factory('$router', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', '$routerRootComponent', routerFactory]);
7+
factory('$rootRouter', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', '$routerRootComponent', routerFactory]);
88

99
function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector, $routerRootComponent) {
1010

modules/angular1_router/src/ng_outlet.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class DirectiveIntrospectorProvider {
6161
*
6262
* The value for the `ngOutlet` attribute is optional.
6363
*/
64-
function ngOutletDirective($animate, $q: ng.IQService, $router) {
65-
let rootRouter = $router;
64+
function ngOutletDirective($animate, $q: ng.IQService, $rootRouter) {
65+
let rootRouter = $rootRouter;
6666

6767
return {
6868
restrict: 'AE',
@@ -231,8 +231,8 @@ function routerTriggerDirective($q) {
231231
*
232232
* ```js
233233
* angular.module('myApp', ['ngComponentRouter'])
234-
* .controller('AppController', ['$router', function($router) {
235-
* $router.config({ path: '/user/:id', component: 'user' });
234+
* .controller('AppController', ['$rootRouter', function($rootRouter) {
235+
* $rootRouter.config({ path: '/user/:id', component: 'user' });
236236
* this.user = { name: 'Brian', id: 123 };
237237
* });
238238
* ```
@@ -243,13 +243,11 @@ function routerTriggerDirective($q) {
243243
* </div>
244244
* ```
245245
*/
246-
function ngLinkDirective($router, $parse) {
247-
let rootRouter = $router;
248-
246+
function ngLinkDirective($rootRouter, $parse) {
249247
return {require: '?^^ngOutlet', restrict: 'A', link: ngLinkDirectiveLinkFn};
250248

251249
function ngLinkDirectiveLinkFn(scope, element, attrs, ctrl) {
252-
let router = (ctrl && ctrl.$$router) || rootRouter;
250+
let router = (ctrl && ctrl.$$router) || $rootRouter;
253251
if (!router) {
254252
return;
255253
}
@@ -277,7 +275,7 @@ function ngLinkDirective($router, $parse) {
277275
return;
278276
}
279277

280-
$router.navigateByInstruction(instruction);
278+
$rootRouter.navigateByInstruction(instruction);
281279
event.preventDefault();
282280
});
283281
}
@@ -291,9 +289,9 @@ function dashCase(str: string): string {
291289
* A module for adding new a routing system Angular 1.
292290
*/
293291
angular.module('ngComponentRouter', [])
294-
.directive('ngOutlet', ['$animate', '$q', '$router', ngOutletDirective])
292+
.directive('ngOutlet', ['$animate', '$q', '$rootRouter', ngOutletDirective])
295293
.directive('ngOutlet', ['$compile', ngOutletFillContentDirective])
296-
.directive('ngLink', ['$router', '$parse', ngLinkDirective])
294+
.directive('ngLink', ['$rootRouter', '$parse', ngLinkDirective])
297295
.directive('$router', ['$q', routerTriggerDirective]);
298296

299297
/*

modules/angular1_router/src/ng_route_shim.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
.directive('a', anchorLinkDirective)
2525

2626
// Connects the legacy $routeProvider config shim to Component Router's config.
27-
.run(['$route', '$router', function ($route, $router) {
27+
.run(['$route', '$rootRouter', function ($route, $rootRouter) {
2828
$route.$$subscribe(function (routeDefinition) {
2929
if (!angular.isArray(routeDefinition)) {
3030
routeDefinition = [routeDefinition];
3131
}
32-
$router.config(routeDefinition);
32+
$rootRouter.config(routeDefinition);
3333
});
3434
}]);
3535

@@ -241,12 +241,12 @@
241241

242242
}
243243

244-
function $routeParamsFactory($router, $rootScope) {
244+
function $routeParamsFactory($rootRouter, $rootScope) {
245245
// the identity of this object cannot change
246246
var paramsObj = {};
247247

248248
$rootScope.$on('$routeChangeSuccess', function () {
249-
var newParams = $router._currentInstruction && $router._currentInstruction.component.params;
249+
var newParams = $rootRouter._currentInstruction && $rootRouter._currentInstruction.component.params;
250250

251251
angular.forEach(paramsObj, function (val, name) {
252252
delete paramsObj[name];
@@ -262,7 +262,7 @@
262262
/**
263263
* Allows normal anchor links to kick off routing.
264264
*/
265-
function anchorLinkDirective($router) {
265+
function anchorLinkDirective($rootRouter) {
266266
return {
267267
restrict: 'E',
268268
link: function (scope, element) {
@@ -281,8 +281,8 @@
281281
}
282282

283283
var href = element.attr(hrefAttrName);
284-
if (href && $router.recognize(href)) {
285-
$router.navigateByUrl(href);
284+
if (href && $rootRouter.recognize(href)) {
285+
$rootRouter.navigateByUrl(href);
286286
event.preventDefault();
287287
}
288288
});

modules/angular1_router/test/integration/animation_spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('ngOutlet animations', function () {
55
$animate,
66
$compile,
77
$rootScope,
8-
$router,
8+
$rootRouter,
99
$compileProvider;
1010

1111
beforeEach(function () {
@@ -17,11 +17,11 @@ describe('ngOutlet animations', function () {
1717
$compileProvider = _$compileProvider_;
1818
});
1919

20-
inject(function (_$animate_, _$compile_, _$rootScope_, _$router_) {
20+
inject(function (_$animate_, _$compile_, _$rootScope_, _$rootRouter_) {
2121
$animate = _$animate_;
2222
$compile = _$compile_;
2323
$rootScope = _$rootScope_;
24-
$router = _$router_;
24+
$rootRouter = _$rootRouter_;
2525
});
2626

2727
registerComponent('userCmp', {
@@ -41,11 +41,11 @@ describe('ngOutlet animations', function () {
4141

4242
compile('<div ng-outlet></div>');
4343

44-
$router.config([
44+
$rootRouter.config([
4545
{ path: '/user/:name', component: 'userCmp' }
4646
]);
4747

48-
$router.navigateByUrl('/user/brian');
48+
$rootRouter.navigateByUrl('/user/brian');
4949
$rootScope.$digest();
5050
expect(elt.text()).toBe('hello brian');
5151

@@ -54,7 +54,7 @@ describe('ngOutlet animations', function () {
5454
expect(item.event).toBe('enter');
5555

5656
// navigate to pete
57-
$router.navigateByUrl('/user/pete');
57+
$rootRouter.navigateByUrl('/user/pete');
5858
$rootScope.$digest();
5959
expect(elt.text()).toBe('hello pete');
6060

0 commit comments

Comments
 (0)