2016-06-30 9 views
4

私は2つの状態でアプリを持っている:角度ui.routerダイナミックUI-SREF

:私は私の全体のアプリで繰り返されるリンクを持っている

  • profile
  • settings

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

ui-srefを動的に変更するにはどうすればよいですか? - 現在のstateParamと一緒に現在の状態を表す(id

解決策を見つける際には、私はui-sref-activeを使いこなすことを念頭に置いておいてください。このようなもの。

+0

ことができますあなたは精巧?ダイナミック変数とstateParam変数の意味を明確にしていません... –

+0

[this](https://github.com/angular-ui/ui-router/wiki/URL-Routing#url-parameters)を参照してください –

+0

Use $ state.href()とng-hrefをここで説明します:[http://stackoverflow.com/questions/24349731/dynamically-set-the-value-of-ui-sref-angularjs](http://stackoverflow.com/questions/24349731/dynamic-set-of-value-of-ui-sref-angularjs) –

答えて

6

私はui-srefが(と)の内部にあるものを式として解析すると思います。

これだけです。

<a ng-repeat="step in steps" ui-sref="{{step.state}}(step.param)"></a> 
+0

これでスニペットを投稿できますか?よく見えますが、うまくいかない場合は... thou –

+1

[状態とパラメータを両方ともにui-srefで動的に変更](http://stackoverflow.com/questions/32997064/change-both-state-and-params-dynamically- in-ui-sref) –

+0

ありがとう!今日は本当に良いと思った... sthを学んだ! –

1

私が最も安全で理解しやすい方法は、単純な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> 

必ず動作すること...

+0

それはできますが、私はそれをすることはできません。ここで私は2つの州を持っていますが、私のアクチュアリーなアプリでは2以上あります。条件の山を作ることはできません。それは良い解決策ですが、私の場合はそうではありません。ありがとう! – user3800799

+0

答えは@Muhammad Usman –

1
$scope.routers = [ 
    { 
    state: 'profile', 
    params: {id: 1} 
    }, 
    { 
    state: 'settings', 
    params: {id: 1} 
    } 
]; 

ビュー:

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

{{router.state}}() - ()も必要です。 –

関連する問題