Skip to content

A reverse function that generates URI string from state name and params? #128

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
liokm opened this issue May 13, 2013 · 4 comments · Fixed by #138
Closed

A reverse function that generates URI string from state name and params? #128

liokm opened this issue May 13, 2013 · 4 comments · Fixed by #138

Comments

@liokm
Copy link

liokm commented May 13, 2013

It would be nice to have a reverse function in $state to map state name and params to corresponding URI. For example:

$stateProvider
  .state('foo.bar', {
    url: '/{fooId}/{barId}'
  });

then
$state.reverse('foo.bar', {fooId:1, barId:2}) generates URI string: '/1/2' (or '#/1/2', depends on html5mode)

Currently, this could be done by using $state.$current.url.format for the current state or ancestor states, but not for the other such as sibling states. Comparing with concatenating strings, this way feels neat and better managed.

@jeme
Copy link
Contributor

jeme commented May 13, 2013

$state.transitionTo doesn't work for you?

AFAIK this should work: $state.transitionTo('foo.bar', {fooId:1, barId:2}) given your example.

@liokm
Copy link
Author

liokm commented May 13, 2013

Thanks for reply. I dont want an instant state transition, but invoking this to generate links. For example:

$scope.getFooLink = function() {
    return $state.reverse('foo', {fooId: some_var});
};

and in view

<a ng-href="{{ getFooLink() }}">foo</a>

@jeme
Copy link
Contributor

jeme commented May 13, 2013

Arh, then it's related to #16 instead i suppose

@ksperling
Copy link
Contributor

Yes, that's essentially #16, but I agree it makes sense to have both a function and a directive (presumably the directive would just be a wrapper around the function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants