ui-router中动态构造的ui-sref属性

时间:2014-05-05 15:31:18

标签: angularjs angular-ui-router

我是角色的新手,特别是ui-router。

这是一个链接:

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

链接是动态填充的。

所以当我尝试从我的配置中访问该状态时:

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

我收到此错误消息:

  

错误:无法解析&#39; / topics / myTopic&#39;来自州&#39;主题&#39;

在上面:myTopic是变量名。

2 个答案:

答案 0 :(得分:31)

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

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

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

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

因为ui-sref表示:ui-sref='stateName({param: value, param: value})。更多信息:

答案 1 :(得分:8)

尝试将状态的简单名称用作'topics',并使用'topics/:topicId'作为url属性。 之后,您可以使用ui-sref='topics({topicId: topic.id})'

相关问题