1-
1+ /**
2+ * @ngdoc object
3+ * @name ui.router.router.$urlRouterProvider
4+ *
5+ * @requires ui.router.util.$urlMatcherFactoryProvider
6+ *
7+ * @description
8+ * `$urlRouterProvider` has the responsibility of watching `$location`.
9+ * When `$location` changes it runs through a list of rules one by one until a
10+ * match is found. `$urlRouterProvider` is used behind the scenes anytime you specify
11+ * a url in a state configuration. All urls are compiled into a UrlMatcher object.
12+ *
13+ * There are several methods on `$urlRouterProvider` that make it useful to use directly
14+ * in your module config.
15+ */
216$UrlRouterProvider . $inject = [ '$urlMatcherFactoryProvider' ] ;
317function $UrlRouterProvider ( $urlMatcherFactory ) {
418 var rules = [ ] ,
@@ -17,13 +31,75 @@ function $UrlRouterProvider( $urlMatcherFactory) {
1731 } ) ;
1832 }
1933
34+ /**
35+ * @ngdoc function
36+ * @name ui.router.router.$urlRouterProvider#rule
37+ * @methodOf ui.router.router.$urlRouterProvider
38+ *
39+ * @description
40+ * Defines rules that are used by `$urlRouterProvider to find matches for
41+ * specific URLs.
42+ *
43+ * @example
44+ * <pre>
45+ * var app = angular.module('app', ['ui.router.router']);
46+ *
47+ * app.config(function ($urlRouterProvider) {
48+ * // Here's an example of how you might allow case insensitive urls
49+ * $urlRouterProvider.rule(function ($injector, $location) {
50+ * var path = $location.path(),
51+ * normalized = path.toLowerCase();
52+ *
53+ * if (path !== normalized) {
54+ * return normalized;
55+ * }
56+ * });
57+ * });
58+ * </pre>
59+ *
60+ * @param {object } rule Handler function that takes `$injector` and `$location`
61+ * services as arguments. You can use them to return a valid path as a string.
62+ *
63+ * @return {object } $urlRouterProvider - $urlRouterProvider instance
64+ */
2065 this . rule =
2166 function ( rule ) {
2267 if ( ! isFunction ( rule ) ) throw new Error ( "'rule' must be a function" ) ;
2368 rules . push ( rule ) ;
2469 return this ;
2570 } ;
2671
72+ /**
73+ * @ngdoc object
74+ * @name ui.router.router.$urlRouterProvider#otherwise
75+ * @methodOf ui.router.router.$urlRouterProvider
76+ *
77+ * @description
78+ * Defines a path that is used when an invalied route is requested.
79+ *
80+ * @example
81+ * <pre>
82+ * var app = angular.module('app', ['ui.router.router']);
83+ *
84+ * app.config(function ($urlRouterProvider) {
85+ * // if the path doesn't match any of the urls you configured
86+ * // otherwise will take care of routing the user to the
87+ * // specified url
88+ * $urlRouterProvider.otherwise('/index');
89+ *
90+ * // Example of using function rule as param
91+ * $urlRouterProvider.otherwise(function ($injector, $location) {
92+ * ...
93+ * });
94+ * });
95+ * </pre>
96+ *
97+ * @param {string|object } rule The url path you want to redirect to or a function
98+ * rule that returns the url path. The function version is passed two params:
99+ * `$injector` and `$location` services.
100+ *
101+ * @return {object } $urlRouterProvider - $urlRouterProvider instance
102+ */
27103 this . otherwise =
28104 function ( rule ) {
29105 if ( isString ( rule ) ) {
@@ -42,6 +118,43 @@ function $UrlRouterProvider( $urlMatcherFactory) {
42118 return isDefined ( result ) ? result : true ;
43119 }
44120
121+ /**
122+ * @ngdoc function
123+ * @name ui.router.router.$urlRouterProvider#when
124+ * @methodOf ui.router.router.$urlRouterProvider
125+ *
126+ * @description
127+ * Registers a handler for a given url matching. if handle is a string, it is
128+ * treated as a redirect, and is interpolated according to the syyntax of match
129+ * (i.e. like String.replace() for RegExp, or like a UrlMatcher pattern otherwise).
130+ *
131+ * If the handler is a function, it is injectable. It gets invoked if `$location`
132+ * matches. You have the option of inject the match object as `$match`.
133+ *
134+ * The handler can return
135+ *
136+ * - **falsy** to indicate that the rule didn't match after all, then `$urlRouter`
137+ * will continue trying to find another one that matches.
138+ * - **string** which is treated as a redirect and passed to `$location.url()`
139+ * - **void** or any **truthy** value tells `$urlRouter` that the url was handled.
140+ *
141+ * @example
142+ * <pre>
143+ * var app = angular.module('app', ['ui.router.router']);
144+ *
145+ * app.config(function ($urlRouterProvider) {
146+ * $urlRouterProvider.when($state.url, function ($match, $stateParams) {
147+ * if ($state.$current.navigable !== state ||
148+ * !equalForKeys($match, $stateParams) {
149+ * $state.transitionTo(state, $match, false);
150+ * }
151+ * });
152+ * });
153+ * </pre>
154+ *
155+ * @param {string|object } what The incoming path that you want to redirect.
156+ * @param {string|object } handler The path you want to redirect your user to.
157+ */
45158 this . when =
46159 function ( what , handler ) {
47160 var redirect , handlerIsString = isString ( handler ) ;
@@ -88,6 +201,17 @@ function $UrlRouterProvider( $urlMatcherFactory) {
88201 throw new Error ( "invalid 'what' in when()" ) ;
89202 } ;
90203
204+ /**
205+ * @ngdoc object
206+ * @name ui.router.router.$urlRouter
207+ *
208+ * @requires $location
209+ * @requires $rootScope
210+ * @requires $injector
211+ *
212+ * @description
213+ *
214+ */
91215 this . $get =
92216 [ '$location' , '$rootScope' , '$injector' ,
93217 function ( $location , $rootScope , $injector ) {
@@ -113,6 +237,23 @@ function $UrlRouterProvider( $urlMatcherFactory) {
113237 $rootScope . $on ( '$locationChangeSuccess' , update ) ;
114238
115239 return {
240+ /**
241+ * @ngdoc function
242+ * @name ui.router.router.$urlRouter#sync
243+ * @methodOf ui.router.router.$urlRouter
244+ *
245+ * @description
246+ * Checks registered rules until first rule is handled.
247+ *
248+ * @example
249+ * <pre>
250+ * var app = angular.module('app', ['ui.router.router']);
251+ *
252+ * app.run(function ($urlRouter) {
253+ * $urlRouter.sync();
254+ * });
255+ * </pre>
256+ */
116257 sync : function ( ) {
117258 update ( ) ;
118259 }
0 commit comments