Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

angularjs - Dynamically constructed ui-sref attribute in ui-router

I am new to angular and especially ui-router.

Here is a link:

<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:

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

In the above: myTopic is a variable name.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: ""
 })

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...