Demas Sinner
Demas Sinner

Reputation: 101

ui-router / ui-sref, inherited parameters?

I have a couple of parameters that are fundamental and often present in my application.

Simplified exemple of url in the router definition:

url:'/settings/:id'
url:'/profile/:id'
url:'/account/:id'

Do I need to pass this :id parameter on every state.go() or ui-sref?

Example:

ui-sref="settings({id:vm.id})"
ui-sref="profile({id:vm.id})"
ui-sref="account({id:vm.id})"

I would be much simpler if there were a way to copy the named url parameters and specify a specific parameter if I would like to replace it or omit it.

Example:

ui-sref="account"

And if and only if I would like to overwrite it:

ui-sref="account({id:vm.anotherId})"

According to the API inherit is the default behaviour: "inherit Boolean (default true), If true will inherit url parameters from current url." https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options

And this applys to ui-sref also according to: "There is no functional difference between ui-sref and $state.go. See the doc" Difference between ui-sref and $state.go in AngularJS UI-Router

This seems trivial, or I misunderstand something :-( I have tried playing around with nested states, but the real application has not the nested states carasteristics.

Upvotes: 0

Views: 790

Answers (1)

Aaron Pool
Aaron Pool

Reputation: 489

I think you need to invert the order of your url, changing it to something like this:

url: user/:id/settings
url: user/:id/profile
url: user/:id/account

It makes more sense this way anyways, you're not looking up the id of a setting, you're looking up the settings of a user/application/website/etc with an id of :id. You can just make the parent state user be abstract and have one of the child states be the default state that navigates to user/:id. That way, the id will be inherited, but you can transition simply by saying ui-sref="^.account" (a relative state transition).

Upvotes: 2

Related Questions