Skip to content

Commit 5ee8d1e

Browse files
committed
Merge pull request #933 from timsly/current-ui-sref
feat(uiSref): extend syntax for ui-sref
2 parents 8802a0d + 71cad3d commit 5ee8d1e

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', '?^uiSrefActiveEq'],
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
@@ -214,6 +214,30 @@ describe('uiStateRef', function() {
214214
expect($state.current.name).toEqual('');
215215
expect($stateParams).toEqual({ id: "5" });
216216
}));
217+
218+
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
219+
$state.current.name = 'contacts.item.detail';
220+
221+
el = angular.element("<a ui-sref=\"{id: $index}\">Details</a>");
222+
$rootScope.$index = 3;
223+
$rootScope.$apply();
224+
225+
$compile(el)($rootScope);
226+
$rootScope.$digest();
227+
expect(el.attr('href')).toBe('#/contacts/3');
228+
}));
229+
230+
it('should allow multi-line attribute values when passing params to current state', inject(function($compile, $rootScope, $state) {
231+
$state.current.name = 'contacts.item.detail';
232+
233+
el = angular.element("<a ui-sref=\"{\n\tid: $index\n}\">Details</a>");
234+
$rootScope.$index = 3;
235+
$rootScope.$apply();
236+
237+
$compile(el)($rootScope);
238+
$rootScope.$digest();
239+
expect(el.attr('href')).toBe('#/contacts/3');
240+
}));
217241
});
218242

219243
describe('forms', function() {

0 commit comments

Comments
 (0)