@@ -31,13 +31,15 @@ function stateContext(el: IAugmentedJQuery) {
31
31
return path ? tail ( path ) . state . name : undefined ;
32
32
}
33
33
34
+ /** @hidden */
34
35
function processedDef ( $state : StateService , $element : IAugmentedJQuery , def : Def ) : Def {
35
36
let uiState = def . uiState || $state . current . name ;
36
37
let uiStateOpts = extend ( defaultOpts ( $element , $state ) , def . uiStateOpts || { } ) ;
37
38
let href = $state . href ( uiState , def . uiStateParams , uiStateOpts ) ;
38
39
return { uiState, uiStateParams : def . uiStateParams , uiStateOpts, href } ;
39
40
}
40
41
42
+ /** @hidden */
41
43
interface TypeInfo {
42
44
attr : string ;
43
45
isAnchor : boolean ;
@@ -91,45 +93,93 @@ function defaultOpts(el: IAugmentedJQuery, $state: StateService) {
91
93
/**
92
94
* `ui-sref`: A directive for linking to a state
93
95
*
94
- * A directive that binds a link (`<a>` tag) to a state.
95
- * If the state has an associated URL, the directive will automatically generate and
96
- * update the `href` attribute via the [[StateService.href]] method.
97
- * Clicking the link will trigger a state transition with optional parameters.
96
+ * A directive which links to a state (and optionally, parameters).
97
+ * When clicked, this directive activates the linked state with the supplied parameter values.
98
+ *
99
+ * ### Linked State
100
+ * The attribute value of the `ui-sref` is the name of the state to link to.
101
+ *
102
+ * #### Example:
103
+ * This will activate the `home` state when the link is clicked.
104
+ * <a ui-sref="home">Home</a>
105
+ *
106
+ * ### Relative Links
107
+ * You can also use relative state paths within `ui-sref`, just like a relative path passed to `$state.go()` ([[StateService.go]]).
108
+ * You just need to be aware that the path is relative to the state that *created* the link.
109
+ * This allows a state to create a relative `ui-sref` which always targets the same destination.
98
110
*
99
- * Also middle-clicking, right-clicking, and ctrl-clicking on the link will be
100
- * handled natively by the browser.
111
+ * #### Example:
112
+ * Both these links are relative to the parent state, even when a child state is currently active.
113
+ * ```html
114
+ * <a ui-sref=".child1">child 1 state</a>
115
+ * <a ui-sref=".child2">child 2 state</a>
116
+ * ```
117
+ *
118
+ * This link activates the parent class.
119
+ * ```html
120
+ * <a ui-sref="^">Return</a>
121
+ * ```
122
+ *
123
+ * ### hrefs
124
+ * If the linked state has a URL, the directive will automatically generate and
125
+ * update the `href` attribute (using the [[StateService.href]] method).
126
+ *
127
+ * #### Example:
128
+ * Assuming the `users` state has a url of `/users/`
129
+ * ```html
130
+ * <a ui-sref="users" href="/users/">Users</a>
131
+ * ```
132
+ *
133
+ * ### Parameter Values
134
+ * In addition to the state name, a `ui-sref` can include parameter values which are applied when activating the state.
135
+ * Param values can be provided in the `ui-sref` value after the state name, enclosed by parentheses.
136
+ * The content inside the parentheses is an expression, evaluated to the parameter values.
137
+ *
138
+ * #### Example:
139
+ * This example renders a list of links to users.
140
+ * The state's `userId` parameter value comes from each user's `user.id` property.
141
+ * ```html
142
+ * <li ng-repeat="user in users">
143
+ * <a ui-sref="users.detail({ userId: user.id })">{{ user.displayName }}</a>
144
+ * </li>
145
+ * ```
101
146
*
102
- * You can also use relative state paths within ui-sref, just like the relative
103
- * paths passed to `$state.go()`.
104
- * You just need to be aware that the path is relative to the state that the link lives in.
105
- * In other words, the state that created the view containing the link.
147
+ * Note:
148
+ * The parameter values expression is `$watch`ed for updates.
106
149
*
107
- * You can specify options to pass to [[StateService.go]] using the `ui-sref-opts` attribute.
150
+ * ### Transition Options
151
+ * You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-sref-opts` attribute.
108
152
* Options are restricted to `location`, `inherit`, and `reload`.
109
153
*
110
- * Here's an example of how you'd use ui-sref and how it would compile.
154
+ * #### Example:
155
+ * ```html
156
+ * <a ui-sref="home" ui-sref-opts="{ reload: true }">Home</a>
157
+ * ```
158
+ *
159
+ * ### Highlighting the active link
160
+ * This directive can be used in conjunction with [[uiSrefActive]] to highlight the active link.
161
+ *
162
+ * ### Examples
111
163
* If you have the following template:
112
164
*
113
- * @example
114
165
* ```html
115
- *
116
- * <pre >
117
- * <a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref=" {page: 2}">Next page</a>
166
+ * <a ui-sref="home">Home</a>
167
+ * <a ui-sref="about">About</a >
168
+ * <a ui-sref="{page: 2}">Next page</a>
118
169
*
119
170
* <ul>
120
171
* <li ng-repeat="contact in contacts">
121
172
* <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
122
173
* </li>
123
174
* </ul>
124
- * </pre>
125
175
* ```
126
176
*
127
- * Then the compiled html would be (assuming Html5Mode is off and current state is contacts) :
177
+ * Then (assuming the current state is ` contacts`) the rendered html including hrefs would be :
128
178
*
129
179
* ```html
130
- *
131
- * <pre >
132
- * <a href="#/home" ui-sref="home">Home</a> | <a href="#/about" ui-sref="about">About</a> | <a href="#/ contacts?page=2" ui-sref="{page: 2}">Next page</a>
180
+ * <a href="#/home" ui-sref="home">Home</a>
181
+ * <a href="#/about" ui-sref="about">About</a >
182
+ * <a href="#/contacts?page=2" ui-sref="{page: 2}">Next page</a>
133
183
*
134
184
* <ul>
135
185
* <li ng-repeat="contact in contacts">
@@ -143,12 +193,23 @@ function defaultOpts(el: IAugmentedJQuery, $state: StateService) {
143
193
* </li>
144
194
* </ul>
145
195
*
146
- * <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
147
- * </pre>
196
+ * <a href="#/home" ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
148
197
* ```
149
198
*
150
- * @param {string } ui-sref 'stateName' can be any valid absolute or relative state
151
- * @param {Object } ui-sref-opts options to pass to [[StateService.go]]
199
+ * ### Notes
200
+ *
201
+ * - You can use `ui-sref` to change **only the parameter values** by omitting the state name and parentheses.
202
+ * #### Example:
203
+ * Sets the `lang` parameter to `en` and remains on the same state.
204
+ *
205
+ * ```html
206
+ * <a ui-sref="{ lang: 'en' }">English</a>
207
+ * ```
208
+ *
209
+ * - A middle-click, right-click, or ctrl-click is handled (natively) by the browser to open the href in a new window, for example.
210
+ *
211
+ * - Unlike the parameter values expression, the state name is not `$watch`ed (for performance reasons).
212
+ * If you need to dynamically update the state being linked to, use the fully dynamic [[uiState]] directive.
152
213
*/
153
214
let uiSref = [ '$uiRouter' , '$timeout' ,
154
215
function $StateRefDirective ( $uiRouter : UIRouter , $timeout : ITimeoutService ) {
@@ -201,36 +262,77 @@ let uiSref = ['$uiRouter', '$timeout',
201
262
} ] ;
202
263
203
264
/**
204
- * `ui-state`: A dynamic version of the `ui-sref` directive
265
+ * `ui-state`: A fully dynamic directive for linking to a state
205
266
*
206
- * Much like ui-sref, but it `$observe`s inputs and `$watch`es/evaluates values.
267
+ * A directive which links to a state (and optionally, parameters).
268
+ * When clicked, this directive activates the linked state with the supplied parameter values.
207
269
*
208
- * The `ui-sref` directive takes a string literal, which is split into 1) state name and 2) parameter values expression.
209
- * It does not `$observe` the input string and `$watch`es only the parameter value expression.
210
- * Because of this, `ui-sref` is fairly lightweight, but does no deal well with with srefs that dynamically change.
270
+ * **This directive is very similar to [[uiSref]], but it `$observe`s and `$watch`es/evaluates all its inputs.**
211
271
*
272
+ * A directive which links to a state (and optionally, parameters).
273
+ * When clicked, this directive activates the linked state with the supplied parameter values.
212
274
*
213
- * On the other hand, the `ui-state` directive is fully dynamic.
214
- * It is useful for building dynamic links, such as data–driven navigation links.
275
+ * ### Linked State
276
+ * The attribute value of `ui-state` is an expression which is `$watch`ed and evaluated as the state to link to.
277
+ * **This is in contrast with `ui-sref`, which takes a state name as a string literal.**
215
278
*
216
- * It consists of three attributes:
279
+ * #### Example:
280
+ * Create a list of links.
281
+ * ```html
282
+ * <li ng-repeat="link in navlinks">
283
+ * <a ui-sref="link.state">{{ link.displayName }}</a>
284
+ * </li>
217
285
*
218
- * - `ui-state="expr"`: The state to link to; the `expr` string is evaluated and `$watch`ed
219
- * - `ui-state-params="expr"`: The state params to link to; the `expr` string is evaluated and `$watch`ed
220
- * - `ui-state-opts="expr"`: The transition options for the link; the `expr` string is evaluated and `$watch`ed
286
+ * ### Relative Links
287
+ * If the expression evaluates to a relative path, it is processed like [[uiSref]].
288
+ * You just need to be aware that the path is relative to the state that *created* the link.
289
+ * This allows a state to create relative `ui-state` which always targets the same destination.
221
290
*
222
- * In angular 1.3 and above, a one time binding may be used if you know specific bindings will not change, i.e:
223
- * `ui-params="::foo.params"`.
291
+ * ### hrefs
292
+ * If the linked state has a URL, the directive will automatically generate and
293
+ * update the `href` attribute (using the [[StateService.href]] method).
224
294
*
225
- * Like `ui-sref`, this directive also works with `ui-sref-active` and `ui-sref-active-eq`.
295
+ * ### Parameter Values
296
+ * In addition to the state name expression, a `ui-state` can include parameter values which are applied when activating the state.
297
+ * Param values should be provided using the `ui-state-params` attribute.
298
+ * The `ui-state-params` attribute value is `$watch`ed and evaluated as an expression.
226
299
*
227
- * @example
300
+ * #### Example:
301
+ * This example renders a list of links with param values.
302
+ * The state's `userId` parameter value comes from each user's `user.id` property.
228
303
* ```html
229
- *
230
- * <li ng-repeat="nav in navlinks" ui-sref-active="active">
231
- * <a ui-state="nav.statename" ui-state-params="nav.params">{{nav.description}}</a>
304
+ * <li ng-repeat="link in navlinks">
305
+ * <a ui-state="link.state" ui-state-params="link.params">{{ link.displayName }}</a>
232
306
* </li>
233
307
* ```
308
+ *
309
+ * ### Transition Options
310
+ * You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-state-opts` attribute.
311
+ * Options are restricted to `location`, `inherit`, and `reload`.
312
+ * The value of the `ui-state-opts` is `$watch`ed and evaluated as an expression.
313
+ *
314
+ * #### Example:
315
+ * ```html
316
+ * <a ui-state="returnto.state" ui-state-opts="{ reload: true }">Home</a>
317
+ * ```
318
+ *
319
+ * ### Highlighting the active link
320
+ * This directive can be used in conjunction with [[uiSrefActive]] to highlight the active link.
321
+ *
322
+ * ### Notes
323
+ *
324
+ * - You can use `ui-params` to change **only the parameter values** by omitting the state name and supplying only `ui-state-params`.
325
+ * However, it might be simpler to use [[uiSref]] parameter-only links.
326
+ *
327
+ * #### Example:
328
+ * Sets the `lang` parameter to `en` and remains on the same state.
329
+ *
330
+ * ```html
331
+ * <a ui-state="" ui-state-params="{ lang: 'en' }">English</a>
332
+ * ```
333
+ *
334
+ * - A middle-click, right-click, or ctrl-click is handled (natively) by the browser to open the href in a new window, for example.
335
+ * ```
234
336
*/
235
337
let uiState = [ '$uiRouter' , '$timeout' ,
236
338
function $StateRefDynamicDirective ( $uiRouter : UIRouter , $timeout : ITimeoutService ) {
@@ -289,82 +391,83 @@ let uiState = ['$uiRouter', '$timeout',
289
391
/**
290
392
* `ui-sref-active` and `ui-sref-active-eq`: A directive that adds a CSS class when a `ui-sref` is active
291
393
*
292
- * A directive working alongside ui-sref to add classes to an element when the
293
- * related ui-sref directive's state is active, and removing them when it is inactive.
294
- * The primary use-case is to simplify the special appearance of navigation menus
295
- * relying on `ui-sref`, by having the " active" state's menu button appear different ,
394
+ * A directive working alongside [[uiSref]] and [[uiState]] to add classes to an element when the
395
+ * related directive's state is active ( and remove them when it is inactive) .
396
+ *
397
+ * The primary use-case is to highlight the active link in navigation menus ,
296
398
* distinguishing it from the inactive menu items.
297
399
*
298
- * ui-sref-active can live on the same element as ui-sref or on a parent element. The first
299
- * ui-sref-active found at the same level or above the ui-sref will be used.
400
+ * ### Linking to a `ui-sref` or `ui-state`
401
+ * `ui-sref-active` can live on the same element as `ui-sref`/`ui-state`, or it can be on a parent element.
402
+ * If a `ui-sref-active` is a parent to more than one `ui-sref`/`ui-state`, it will apply the CSS class when **any of the links are active**.
403
+ *
404
+ * ### Matching
405
+ *
406
+ * The `ui-sref-active` directive applies the CSS class when the `ui-sref`/`ui-state`'s target state **or any child state is active**.
407
+ * This is a "fuzzy match" which uses [[StateService.includes]].
408
+ *
409
+ * The `ui-sref-active-eq` directive applies the CSS class when the `ui-sref`/`ui-state`'s target state is directly active (not when child states are active).
410
+ * This is an "exact match" which uses [[StateService.is]].
411
+ *
412
+ * ### Parameter values
413
+ * If the `ui-sref`/`ui-state` includes parameter values, the current parameter values must match the link's values for the link to be highlighted.
414
+ * This allows a list of links to the same state with different parameters to be rendered, and the correct one highlighted.
415
+ *
416
+ * #### Example:
417
+ * ```html
418
+ * <li ng-repeat="user in users" ui-sref-active="active">
419
+ * <a ui-sref="user.details({ userId: user.id })">{{ user.lastName }}</a>
420
+ * </li>
421
+ * ```
300
422
*
301
- * Will activate when the ui-sref's target state or any child state is active. If you
302
- * need to activate only when the ui-sref target state is active and *not* any of
303
- * it's children, then you will use ui-sref-active-eq
423
+ * ### Examples
304
424
*
305
425
* Given the following template:
306
426
* @example
307
427
* ```html
308
- *
309
- * <pre>
310
428
* <ul>
311
429
* <li ui-sref-active="active" class="item">
312
430
* <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
313
431
* </li>
314
432
* </ul>
315
- * </pre>
316
433
* ```
317
434
*
318
- *
319
- * When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins",
435
+ * When the app state is `app.user` (or any child state),
436
+ * and contains the state parameter "user" with value "bilbobaggins",
320
437
* the resulting HTML will appear as (note the 'active' class):
321
438
*
322
439
* ```html
323
- *
324
- * <pre>
325
440
* <ul>
326
441
* <li ui-sref-active="active" class="item active">
327
442
* <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
328
443
* </li>
329
444
* </ul>
330
- * </pre>
331
445
* ```
332
446
*
333
- * The class name is interpolated **once** during the directives link time (any further changes to the
334
- * interpolated value are ignored).
335
- *
336
- * Multiple classes may be specified in a space-separated format:
447
+ * ### Glob mode
337
448
*
338
- * ```html
339
- * <pre>
340
- * <ul>
341
- * <li ui-sref-active='class1 class2 class3'>
342
- * <a ui-sref="app.user">link</a>
343
- * </li>
344
- * </ul>
345
- * </pre>
346
- * ```
347
- *
348
- * It is also possible to pass ui-sref-active an expression that evaluates
349
- * to an object hash, whose keys represent active class names and whose
350
- * values represent the respective state names/globs.
351
- * ui-sref-active will match if the current active state **includes** any of
449
+ * It is possible to pass `ui-sref-active` an expression that evaluates to an object.
450
+ * The objects keys represent active class names and values represent the respective state names/globs.
451
+ * `ui-sref-active` will match if the current active state **includes** any of
352
452
* the specified state names/globs, even the abstract ones.
353
453
*
454
+ * #### Example:
354
455
* Given the following template, with "admin" being an abstract state:
355
- * @example
356
456
* ```html
357
- *
358
- * <pre>
359
- * <div ui-sref-active="{'active': 'admin.*'}">
457
+ * <div ui-sref-active="{'active': 'admin.**'}">
360
458
* <a ui-sref-active="active" ui-sref="admin.roles">Roles</a>
361
459
* </div>
362
- * </pre>
363
460
* ```
364
461
*
365
- * When the current state is "admin.roles" the "active" class will be applied
366
- * to both the <div> and <a> elements. It is important to note that the state
367
- * names/globs passed to ui-sref-active shadow the state provided by ui-sref.
462
+ * When the current state is "admin.roles" the "active" class will be applied to both the <div> and <a> elements.
463
+ * It is important to note that the state names/globs passed to `ui-sref-active` override any state provided by a linked `ui-sref`.
464
+ *
465
+ * ### Notes:
466
+ *
467
+ * - The class name is interpolated **once** during the directives link time (any further changes to the
468
+ * interpolated value are ignored).
469
+ *
470
+ * - Multiple classes may be specified in a space-separated format: `ui-sref-active='class1 class2 class3'`
368
471
*/
369
472
let uiSrefActive = [ '$state' , '$stateParams' , '$interpolate' , '$uiRouter' ,
370
473
function $StateRefActiveDirective ( $state : StateService , $stateParams : Obj , $interpolate : IInterpolateService , $uiRouter : UIRouter ) {
0 commit comments