Skip to content

Allow state options to be overidden in ui-sref directive #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/stateDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function stateContext(el) {
* to the state that the link lives in, in other words the state that loaded the
* template containing the link.
*
* Options passed to the `$state.go()` also can be overidden with the `ui-sref-opts`
* attribute.
*
* @example
* <pre>
* <a ui-sref="home">Home</a> | <a ui-sref="about">About</a>
Expand All @@ -44,6 +47,8 @@ function stateContext(el) {
* <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
* </li>
* </ul>
*
* <a ui-sref="home" ui-sref-opts="{location: 'replace'}">Home</a>
* </pre>
*
* @param {string} ui-sref 'stateName' can be any valid absolute or relative state
Expand All @@ -58,12 +63,16 @@ function $StateRefDirective($state, $timeout) {
var params = null, url = null, base = stateContext(element) || $state.$current;
var isForm = element[0].nodeName === "FORM";
var attr = isForm ? "action" : "href", nav = true;
var opts = angular.extend(
{ relative: base },
scope.$eval(attrs.uiSrefOpts) || {}
);

var update = function(newVal) {
if (newVal) params = newVal;
if (!nav) return;

var newHref = $state.href(ref.state, params, { relative: base });
var newHref = $state.href(ref.state, params, opts);

if (!newHref) {
nav = false;
Expand Down Expand Up @@ -91,7 +100,7 @@ function $StateRefDirective($state, $timeout) {
// HACK: This is to allow ng-clicks to be processed before the transition is initiated:
$timeout(function() {
scope.$apply(function() {
$state.go(ref.state, params, { relative: base });
$state.go(ref.state, params, opts);
});
});
e.preventDefault();
Expand Down
12 changes: 12 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ describe('uiStateRef', function() {
expect(el.attr('href')).toBe('#/contacts/3');
}));

it('should allow to override options', inject(function($compile, $rootScope, $state) {
el = angular.element('<a ui-sref=".detail({id: $index})" ui-sref-opts="{relative: $relativeState}">Details</a>');
$rootScope.$index = 3;
$rootScope.$relativeState = $state.get('contacts.item');
$rootScope.$apply();

$compile(el)($rootScope);
$rootScope.$digest();

expect(el.attr('href')).toBe('#/contacts/3');
}));

it('should transition states when left-clicked', inject(function($state, $stateParams, $document, $q, $timeout) {
expect($state.$current.name).toEqual('');

Expand Down