From b21e9aab5079490686b8b0058c80f5de71abfff4 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Tue, 17 Dec 2013 17:07:17 +0100 Subject: [PATCH 1/2] Allow state options in ui-sref directive (ui-state-opts) --- src/stateDirectives.js | 13 +++++++++++-- test/stateDirectivesSpec.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/stateDirectives.js b/src/stateDirectives.js index a2f9f139d..8862f9923 100644 --- a/src/stateDirectives.js +++ b/src/stateDirectives.js @@ -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 overiden with the `ui-sref-opts` + * attribute. + * * @example *
  * Home | About
@@ -44,6 +47,8 @@ function stateContext(el) {
  *     {{ contact.name }}
  *   
  * 
+ *
+ * Home
  * 
* * @param {string} ui-sref 'stateName' can be any valid absolute or relative state @@ -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; @@ -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(); diff --git a/test/stateDirectivesSpec.js b/test/stateDirectivesSpec.js index f25460fba..3e6c420f4 100644 --- a/test/stateDirectivesSpec.js +++ b/test/stateDirectivesSpec.js @@ -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('Details'); + $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(''); From b2b6552b44b0dd87e5c45066ad6fcf468e564fcf Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Fri, 3 Jan 2014 11:26:50 +0100 Subject: [PATCH 2/2] fix comment typo --- src/stateDirectives.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stateDirectives.js b/src/stateDirectives.js index 8862f9923..103ee6a48 100644 --- a/src/stateDirectives.js +++ b/src/stateDirectives.js @@ -35,7 +35,7 @@ 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 overiden with the `ui-sref-opts` + * Options passed to the `$state.go()` also can be overidden with the `ui-sref-opts` * attribute. * * @example