在UI路由器动态构建UI的SREF属性 [英] Dynamically constructed ui-sref attribute in ui-router

查看:250
本文介绍了在UI路由器动态构建UI的SREF属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角度,尤其是UI的路由器。

I am new to angular and especially ui-router.

下面是一个链接:

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

该链接动态填充。

The link is dynamically populated.

所以,当我试图从我的配置访问的状态是这样的:

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

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

我收到此错误信息:

I get this error message:

错误:无法解析从状态'主题''/主题/ myTopic

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

在上面: myTopic 是一个变量名

推荐答案

您几乎没有。就在参数必须是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重复的一部分)

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的({参数:值,参数:值})。更多资讯:

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

  • ui router, URL Parameters
  • ui-sref

这篇关于在UI路由器动态构建UI的SREF属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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