Skip to content

Commit 71cad3d

Browse files
committed
feat(uiSref): extend syntax for ui-sref
add possibility to pass params to current state without entering state name
1 parent 4d74d98 commit 71cad3d

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/stateDirectives.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function parseStateRef(ref) {
2-
var parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
1+
function parseStateRef(ref, current) {
2+
var preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed;
3+
if (preparsed) ref = current + '(' + preparsed[1] + ')';
4+
parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
35
if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
46
return { state: parsed[1], paramExpr: parsed[3] || null };
57
}
@@ -43,7 +45,7 @@ function stateContext(el) {
4345
* Here's an example of how you'd use ui-sref and how it would compile. If you have the
4446
* following template:
4547
* <pre>
46-
* <a ui-sref="home">Home</a> | <a ui-sref="about">About</a>
48+
* <a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref="{page: 2}">Next page</a>
4749
*
4850
* <ul>
4951
* <li ng-repeat="contact in contacts">
@@ -52,9 +54,9 @@ function stateContext(el) {
5254
* </ul>
5355
* </pre>
5456
*
55-
* Then the compiled html would be (assuming Html5Mode is off):
57+
* Then the compiled html would be (assuming Html5Mode is off and current state is contacts):
5658
* <pre>
57-
* <a href="#/home" ui-sref="home">Home</a> | <a href="#/about" ui-sref="about">About</a>
59+
* <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>
5860
*
5961
* <ul>
6062
* <li ng-repeat="contact in contacts">
@@ -82,7 +84,7 @@ function $StateRefDirective($state, $timeout) {
8284
restrict: 'A',
8385
require: '?^uiSrefActive',
8486
link: function(scope, element, attrs, uiSrefActive) {
85-
var ref = parseStateRef(attrs.uiSref);
87+
var ref = parseStateRef(attrs.uiSref, $state.current.name);
8688
var params = null, url = null, base = stateContext(element) || $state.$current;
8789
var isForm = element[0].nodeName === "FORM";
8890
var attr = isForm ? "action" : "href", nav = true;

test/stateDirectivesSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ describe('uiStateRef', function() {
184184
expect($state.current.name).toEqual('');
185185
expect($stateParams).toEqual({ id: "5" });
186186
}));
187+
188+
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
189+
$state.current.name = 'contacts.item.detail';
190+
191+
el = angular.element("<a ui-sref=\"{id: $index}\">Details</a>");
192+
$rootScope.$index = 3;
193+
$rootScope.$apply();
194+
195+
$compile(el)($rootScope);
196+
$rootScope.$digest();
197+
expect(el.attr('href')).toBe('#/contacts/3');
198+
}));
199+
200+
it('should allow multi-line attribute values when passing params to current state', inject(function($compile, $rootScope, $state) {
201+
$state.current.name = 'contacts.item.detail';
202+
203+
el = angular.element("<a ui-sref=\"{\n\tid: $index\n}\">Details</a>");
204+
$rootScope.$index = 3;
205+
$rootScope.$apply();
206+
207+
$compile(el)($rootScope);
208+
$rootScope.$digest();
209+
expect(el.attr('href')).toBe('#/contacts/3');
210+
}));
187211
});
188212

189213
describe('forms', function() {

0 commit comments

Comments
 (0)