Skip to content

Commit f3760c6

Browse files
committed
angular-route: Update types and docs for IRoute
1 parent ffc8351 commit f3760c6

File tree

2 files changed

+108
-26
lines changed

2 files changed

+108
-26
lines changed

types/angular-route/angular-route-tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ $routeProvider
3939
return "I return a string"
4040
}
4141
})
42+
.when('/projects/:projectId/dashboard6', {
43+
resolve: {
44+
foo: () => 'foo',
45+
bar: () => 'bar',
46+
},
47+
resolveAs: 'baz',
48+
resolveRedirectTo: [
49+
'$http',
50+
($http: ng.IHttpService) => $http.get('/is-admin').then(() => '/admin/lounge', () => undefined),
51+
],
52+
})
53+
.when('/projects/:projectId/dashboard7', {
54+
reloadOnUrl: false,
55+
resolveRedirectTo: () => (Math.random() < 0.5) ? '/some/route' : undefined,
56+
})
4257
.otherwise({ redirectTo: '/' })
4358
.otherwise({ redirectTo: ($routeParams?: ng.route.IRouteParamsService, $locationPath?: string, $locationSearch?: any) => "" })
4459
.otherwise("/");

types/angular-route/index.d.ts

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Type definitions for Angular JS (ngRoute module) 1.3
1+
// Type definitions for Angular JS (ngRoute module) 1.7
22
// Project: http://angularjs.org
33
// Definitions by: Jonathan Park <https://github.com/park9140>
4+
// George Kalpakas <https://github.com/gkalpak>
45
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
56
// TypeScript Version: 2.3
67

@@ -52,7 +53,7 @@ declare module 'angular' {
5253
*/
5354
interface IRoute {
5455
/**
55-
* {(string|function()=}
56+
* {(string|Function)=}
5657
* Controller fn that should be associated with newly created scope or the name of a registered controller if passed as a string.
5758
*/
5859
controller?: string | InlineAnnotatedFunction;
@@ -61,56 +62,122 @@ declare module 'angular' {
6162
*/
6263
controllerAs?: string;
6364
/**
64-
* Undocumented?
65-
*/
66-
name?: string;
67-
/**
68-
* {string=|function()=}
65+
* {(string|Function)=}
6966
* Html template as a string or a function that returns an html template as a string which should be used by ngView or ngInclude directives. This property takes precedence over templateUrl.
7067
*
7168
* If template is a function, it will be called with the following parameters:
7269
*
7370
* {Array.<Object>} - route parameters extracted from the current $location.path() by applying the current route
7471
*/
75-
template?: string | { ($routeParams?: angular.route.IRouteParamsService): string; }
72+
template?: string | { ($routeParams?: IRouteParamsService): string; }
7673
/**
77-
* {string=|function()=}
74+
* {(string|Function)=}
7875
* Path or function that returns a path to an html template that should be used by ngView.
7976
*
8077
* If templateUrl is a function, it will be called with the following parameters:
8178
*
8279
* {Array.<Object>} - route parameters extracted from the current $location.path() by applying the current route
8380
*/
84-
templateUrl?: string | { ($routeParams?: angular.route.IRouteParamsService): string; }
81+
templateUrl?: string | { ($routeParams?: IRouteParamsService): string; }
8582
/**
86-
* {Object.<string, function>=} - An optional map of dependencies which should be injected into the controller. If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. If any of the promises are rejected the $routeChangeError event is fired. The map object is:
87-
*
88-
* - key - {string}: a name of a dependency to be injected into the controller.
89-
* - factory - {string|function}: If string then it is an alias for a service. Otherwise if function, then it is injected and the return value is treated as the dependency. If the result is a promise, it is resolved before its value is injected into the controller. Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. Use $route.current.params to access the new route parameters, instead.
83+
* {Object.<string, Function>=}
84+
* An optional map of dependencies which should be injected into the controller. If any of these
85+
* dependencies are promises, the router will wait for them all to be resolved or one to be rejected before
86+
* the controller is instantiated.
87+
* If all the promises are resolved successfully, the values of the resolved promises are injected and
88+
* `$routeChangeSuccess` event is fired. If any of the promises are rejected the `$routeChangeError` event
89+
* is fired.
90+
* For easier access to the resolved dependencies from the template, the `resolve` map will be available on
91+
* the scope of the route, under `$resolve` (by default) or a custom name specified by the `resolveAs`
92+
* property (see below). This can be particularly useful, when working with components as route templates.
93+
*
94+
* > **Note:** If your scope already contains a property with this name, it will be hidden or overwritten.
95+
* > Make sure, you specify an appropriate name for this property, that does not collide with other
96+
* > properties on the scope.
97+
*
98+
* The map object is:
99+
*
100+
* - `key` – `{string}`: a name of a dependency to be injected into the controller.
101+
* - `factory` - `{string|Function}`: If `string` then it is an alias for a service. Otherwise if function,
102+
* then it is called with `$injector#invoke()` and the return value is treated as the dependency. If the
103+
* result is a promise, it is resolved before its value is injected into the controller. Be aware that
104+
* `ngRoute.$routeParams` will still refer to the previous route within these resolve functions. Use
105+
* `$route.current.params` to access the new route parameters, instead.
90106
*/
91107
resolve?: { [key: string]: any };
92108
/**
93-
* {(string|function())=}
94-
* Value to update $location path with and trigger route redirection.
109+
* {string=}
110+
* The name under which the `resolve` map will be available on the scope of the route. If omitted, defaults
111+
* to `$resolve`.
112+
*/
113+
resolveAs?: string;
114+
/**
115+
* {(string|Function)=}
116+
* Value to update `$location` path with and trigger route redirection.
117+
*
118+
* If `redirectTo` is a function, it will be called with the following parameters:
119+
*
120+
* - `{Object.<string>}` - route parameters extracted from the current `$location.path()` by applying the
121+
* current route templateUrl.
122+
* - `{string}` - current `$location.path()`
123+
* - `{Object}` - current `$location.search()`
124+
*
125+
* The custom `redirectTo` function is expected to return a string which will be used to update
126+
* `$location.url()`. If the function throws an error, no further processing will take place and the
127+
* `$routeChangeError` event will be fired.
128+
*
129+
* Routes that specify `redirectTo` will not have their controllers, template functions or resolves called,
130+
* the `$location` will be changed to the redirect url and route processing will stop. The exception to this
131+
* is if the `redirectTo` is a function that returns `undefined`. In this case the route transition occurs
132+
* as though there was no redirection.
133+
*/
134+
redirectTo?: string | { ($routeParams?: IRouteParamsService, $locationPath?: string, $locationSearch?: any): string };
135+
/**
136+
* {Function=}
137+
* A function that will (eventually) return the value to update `$location` URL with and trigger route
138+
* redirection. In contrast to `redirectTo`, dependencies can be injected into `resolveRedirectTo` and the
139+
* return value can be either a string or a promise that will be resolved to a string.
140+
*
141+
* Similar to `redirectTo`, if the return value is `undefined` (or a promise that gets resolved to
142+
* `undefined`), no redirection takes place and the route transition occurs as though there was no
143+
* redirection.
144+
*
145+
* If the function throws an error or the returned promise gets rejected, no further processing will take
146+
* place and the `$routeChangeError` event will be fired.
147+
*
148+
* `redirectTo` takes precedence over `resolveRedirectTo`, so specifying both on the same route definition,
149+
* will cause the latter to be ignored.
150+
*/
151+
resolveRedirectTo?: angular.Injectable<(...deps: any[]) => angular.IPromise<string | undefined> | string | undefined>;
152+
/**
153+
* {boolean=true}
154+
* Reload route when any part of the URL changes (including the path) even if the new URL maps to the same
155+
* route.
95156
*
96-
* If redirectTo is a function, it will be called with the following parameters:
157+
* If the option is set to `false` and the URL in the browser changes, but the new URL maps to the same
158+
* route, then a `$routeUpdate` event is broadcasted on the root scope (without reloading the route).
97159
*
98-
* - {Object.<string>} - route parameters extracted from the current $location.path() by applying the current route templateUrl.
99-
* - {string} - current $location.path()
100-
* - {Object} - current $location.search()
101-
* - The custom redirectTo function is expected to return a string which will be used to update $location.path() and $location.search().
160+
* Defaults to `true`.
102161
*/
103-
redirectTo?: string | { ($routeParams?: angular.route.IRouteParamsService, $locationPath?: string, $locationSearch?: any): string };
162+
reloadOnUrl?: boolean;
104163
/**
105-
* Reload route when only $location.search() or $location.hash() changes.
164+
* {boolean=true}
165+
* Reload route when only `$location.search()` or `$location.hash()` changes.
166+
*
167+
* If the option is set to `false` and the URL in the browser changes, then a `$routeUpdate` event is
168+
* broadcasted on the root scope (without reloading the route).
169+
*
170+
* > Note: This option has no effect if `reloadOnUrl` is set to `false`.
106171
*
107-
* This option defaults to true. If the option is set to false and url in the browser changes, then $routeUpdate event is broadcasted on the root scope.
172+
* Defaults to `true`.
108173
*/
109174
reloadOnSearch?: boolean;
110175
/**
111-
* Match routes without being case sensitive
176+
* {boolean=false}
177+
* Match routes without being case sensitive.
178+
* If the option is set to `true`, then the particular route can be matched without being case sensitive.
112179
*
113-
* This option defaults to false. If the option is set to true, then the particular route can be matched without being case sensitive
180+
* Defaults to `false`.
114181
*/
115182
caseInsensitiveMatch?: boolean;
116183
}

0 commit comments

Comments
 (0)