问题描述
使用 ui-router 我想在 ng-repeat 中动态设置 ui-sref 指令,如下所示:
Using ui-router I would like to set the ui-sref directive dynamically inside an ng-repeat, like so:
<a ng-repeat="step in steps" ui-sref="{{step.state(step.param)}}"></a>
其中 steps 是一个状态对象"数组,每个对象都有自己的状态和参数对象:
Where steps is an array of "state objects" each with its own state and param object:
var steps = [{state: 'foo', param: {id: 'bar'}}, {...}];
这会引发插值错误.另一方面,仅动态传递状态或参数在下面效果很好:
That throws an interpolation error. On the other hand, passing only the state OR param dynamically works great per below:
// Pass only dynamic state works great
<a ng-repeat="step in steps" ui-sref="{{step.state}}"></a>
// Pass only dynamic param works great
<a ng-repeat="step in steps" ui-sref="foo(step.param)"></a>
我尝试使用 ng-click 作为一种变态的解决方法,但它与 ui-sref-active 配合得不好:
I tried using ng-click as a hacky workaround but it didn't play well with the ui-sref-active:
<a ng-repeat="step in steps" ng-click="$state.go(step.state, step.param)"></a>
有没有人熟悉动态传递状态和参数的好方法?
Is anyone familiar with a good way to pass both state and param dynamically?
推荐答案
我认为 ui-sref 会将 (
和 )
中的内容解析为表达式.
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>
不确定以上是否可行,但这是我会尝试的第一件事.
Not sure if the above would work, but that's the first thing I would try.
这篇关于在 ui-sref 中动态更改状态和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!