ui-router中动态构造的ui-sref属性 [英] Dynamically constructed ui-sref attribute in ui-router

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

问题描述

我是 angular 的新手,尤其是 ui-router.

I am new to angular and especially ui-router.

这是一个链接:

<a ui-sref="/topic/{{topic.id}}">SomeText</a>

链接是动态填充的.

因此,当我尝试从我的配置中访问该状态时:

So when I try to access that state from my config like this:

 .state('topics/:topicId',{
        url:"",
        templateUrl: "",
        controller: ""
    })

我收到此错误消息:

错误:无法从状态topics"解析/topics/myTopic"

Error: Could not resolve '/topics/myTopic' from state 'topics'

在上面:myTopic 是一个变量名.

In the above: myTopic is a variable name.

推荐答案

您就快到了.只是参数必须是 URL 定义的一部分,而不是状态的名称:

You are almost there. Just the parameter must be part of the URL definition, not the name of the state:

.state('topics',{
    url: '/{id:[0-9]{1,8}}', // we can also add some constraint, like int id only
    templateUrl: "",
    controller: ""
 })

以及如何调用它(其中 currentItem.id 将作为某些 ng-repeat 的一部分动态注入)

And how to call it (where currentItem.id would be injected dynamically as a part of some ng-repeat)

<a ui-sref="topics({id:currentItem.id})">SomeText</a>

因为 ui-sref 的意思是:ui-sref='stateName({param: value, param: value}).更多信息在这里:

Because ui-sref means: ui-sref='stateName({param: value, param: value}). More info here:

这篇关于ui-router中动态构造的ui-sref属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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