15

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.

Chuck
  • 998
  • 8
  • 17
  • 30
user3283104
  • 416
  • 3
  • 5
  • 14

2 Answers2

31

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:

BatteryAcid
  • 8,381
  • 5
  • 28
  • 40
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
8

Try to use a simple name for state as 'topics' and use 'topics/:topicId' as url property. After that you can use ui-sref='topics({topicId: topic.id})'

Oleg
  • 9,341
  • 2
  • 43
  • 58
wilver
  • 2,106
  • 1
  • 19
  • 26