在ui-sref中动态更改状态和参数 [英] Change both state and params dynamically in ui-sref

查看:86
本文介绍了在ui-sref中动态更改状态和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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 是状态对象"的数组,每个对象都有自己的状态和param对象:

Where steps is an array of "state objects" each with its own state and param object:

var steps = [{state: 'foo', param: {id: 'bar'}}, {...}];

这将引发插值错误.另一方面,仅动态传递状态OR参数在以下情况下效果很好:

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中动态更改状态和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆