ui-sref
ui.router.state
A directive that binds a link (<a>
tag) to a state. If the state has an associated
URL, the directive will automatically generate & update the href
attribute via
the $state.href() method. Clicking
the link will trigger a state transition with optional parameters.
Also middle-clicking, right-clicking, and ctrl-clicking on the link will be handled natively by the browser.
You can also use relative state paths within ui-sref, just like the relative
paths passed to $state.go()
. You just need to be aware that the path is relative
to the state that the link lives in, in other words the state that loaded the
template containing the link.
You can specify options to pass to $state.go()
using the ui-sref-opts
attribute. Options are restricted to location
, inherit
,
and reload
.
<ANY ui-sref="{string}" ui-sref-opts="{Object}"> ... </ANY>
Param | Type | Details |
---|---|---|
ui-sref | string | 'stateName' can be any valid absolute or relative state |
ui-sref-opts | Object | options to pass to $state.go() |
Here's an example of how you'd use ui-sref and how it would compile. If you have the following template:
<a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref="{page: 2}">Next page</a> <ul> <li ng-repeat="contact in contacts"> <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a> </li> </ul>
Then the compiled html would be (assuming Html5Mode is off and current state is contacts):
<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> <ul> <li ng-repeat="contact in contacts"> <a href="#/contacts/1" ui-sref="contacts.detail({ id: contact.id })">Joe</a> </li> <li ng-repeat="contact in contacts"> <a href="#/contacts/2" ui-sref="contacts.detail({ id: contact.id })">Alice</a> </li> <li ng-repeat="contact in contacts"> <a href="#/contacts/3" ui-sref="contacts.detail({ id: contact.id })">Bob</a> </li> </ul> <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>