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.

user3800799
  • 534
  • 1
  • 5
  • 18
  • Can you elaborate? It's not clear what you mean with dynamic and the stateParam variables... – Alvaro Silvino Jun 30 '16 at 03:38
  • see [this](https://github.com/angular-ui/ui-router/wiki/URL-Routing#url-parameters) – xkeshav Jun 30 '16 at 03:39
  • Use $state.href() and ng-href as explained here: [http://stackoverflow.com/questions/24349731/dynamically-set-the-value-of-ui-sref-angularjs](http://stackoverflow.com/questions/24349731/dynamically-set-the-value-of-ui-sref-angularjs) – Artem K. Jun 30 '16 at 03:48

3 Answers3

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>
Muhammad Usman
  • 345
  • 1
  • 11
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...

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
  • 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! – user3800799 Jun 30 '16 at 03:44
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>
Huy Chau
  • 2,178
  • 19
  • 26