4

I have an app with 2 states:

  • profile
  • settings

I have a link that repeats in my whole app:

<a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a>

How can I change the ui-sref to be dynamic? - to represent the current state along with the current stateParam that I want (which is id)

when finding a solution, keep in mind that I want to be abble to use ui-sref-active so I'd rather avoid ng-click on something like this.

3

3 Answers 3

6

I think ui-sref will parse what is inside the ( and ) as an expression.

So all you have to do is this.

<a ng-repeat="step in steps" ui-sref="{{step.state}}(step.param)"></a>
Sign up to request clarification or add additional context in comments.

2 Comments

can you post a snippet with this? Look good but not sure if will work...thou
1

I guess the most safe and understandable way is using simple if else:

<div ng-if="checkState == 'profile'">
      <a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a>
</div>

<div ng-if="checkState == 'settings'">
      <a ui-sref="settings({id:profile.id})" ui-sref-active="active">Current state + ID</a>
</div>

sure that will work...

1 Comment

It will, but I can't do that. Here, I have 2 states but in my actuall app I have more than 2. I can't create mountains of conditions. It's a good solution, but not for my case. Thanks!
1
$scope.routers = [
  {
    state: 'profile',
    params: {id: 1}
  },
  {
    state: 'settings',
    params: {id: 1}
  } 
];

View:

<a ng-repeat="router in routers" ui-sref="{{router.state}}(router.params)"></a>

1 Comment

{{router.state}}() -- () brackets also needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.