Skip to content

Commit 2ed7a72

Browse files
author
Rory Fitzpatrick
committed
feat(uiSref): add support for transition options
Allows optionally specifying options to be passed to $state.go via attribute ui-sref-opts.
1 parent cf34271 commit 2ed7a72

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/stateDirectives.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ function stateContext(el) {
3535
* to the state that the link lives in, in other words the state that loaded the
3636
* template containing the link.
3737
*
38+
* You can specify options to pass to {@link ui.router.state.$state#go $state.go()}
39+
* using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`,
40+
* and `reload`.
41+
*
3842
* @example
3943
* <pre>
4044
* <a ui-sref="home">Home</a> | <a ui-sref="about">About</a>
@@ -44,12 +48,17 @@ function stateContext(el) {
4448
* <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
4549
* </li>
4650
* </ul>
51+
*
52+
* <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
4753
* </pre>
4854
*
4955
* @param {string} ui-sref 'stateName' can be any valid absolute or relative state
56+
* @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#go $state.go()}
5057
*/
5158
$StateRefDirective.$inject = ['$state', '$timeout'];
5259
function $StateRefDirective($state, $timeout) {
60+
var allowedOptions = ['location', 'inherit', 'reload'];
61+
5362
return {
5463
restrict: 'A',
5564
require: '?^uiSrefActive',
@@ -59,11 +68,21 @@ function $StateRefDirective($state, $timeout) {
5968
var isForm = element[0].nodeName === "FORM";
6069
var attr = isForm ? "action" : "href", nav = true;
6170

71+
var options = {
72+
relative: base
73+
};
74+
var optionsOverride = scope.$eval(attrs.uiSrefOpts) || {};
75+
angular.forEach(allowedOptions, function(option) {
76+
if (option in optionsOverride) {
77+
options[option] = optionsOverride[option];
78+
}
79+
});
80+
6281
var update = function(newVal) {
6382
if (newVal) params = newVal;
6483
if (!nav) return;
6584

66-
var newHref = $state.href(ref.state, params, { relative: base });
85+
var newHref = $state.href(ref.state, params, options);
6786

6887
if (uiSrefActive) {
6988
uiSrefActive.$$setStateInfo(ref.state, params);
@@ -90,7 +109,7 @@ function $StateRefDirective($state, $timeout) {
90109
if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {
91110
// HACK: This is to allow ng-clicks to be processed before the transition is initiated:
92111
$timeout(function() {
93-
$state.go(ref.state, params, { relative: base });
112+
$state.go(ref.state, params, options);
94113
});
95114
e.preventDefault();
96115
}

test/stateDirectivesSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,32 @@ describe('uiStateRef', function() {
257257
expect($state.$current.name).toBe("contacts");
258258
}));
259259
});
260+
261+
describe('transition options', function() {
262+
263+
beforeEach(inject(function($rootScope, $compile, $state) {
264+
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id })" ui-sref-opts="{ reload: true, notify: true }">Details</a>');
265+
scope = $rootScope;
266+
scope.contact = { id: 5 };
267+
268+
$compile(el)(scope);
269+
scope.$digest();
270+
}));
271+
272+
it('uses allowed transition options', inject(function($q, $timeout, $state) {
273+
var transitionOptions;
274+
275+
spyOn($state, 'go').andCallFake(function(state, params, options) {
276+
transitionOptions = options;
277+
});
278+
279+
triggerClick(el);
280+
$timeout.flush();
281+
282+
expect(transitionOptions.reload).toEqual(true);
283+
expect(transitionOptions.notify).toBeUndefined();
284+
}));
285+
});
260286
});
261287

262288
describe('uiSrefActive', function() {

0 commit comments

Comments
 (0)