ui-sref and variable state parameter names

2019-06-22 13:55发布

I want to render a link such as:

<a ui-sref="myState({myKey: 'my variable type value'})">

where state name myState and key myKey are variables.

Is there a way to accomplish this?

6条回答
SAY GOODBYE
2楼-- · 2019-06-22 14:32

I found myself in the same situation, I couldn't acomplish that as well, try to move your code using ng-click and inside of your ng-click function use a $stage.go and pass the parameters there, something like:

$scope.clickHandler = function(param){
    $state.go('state.name',{myKey:param});
}
查看更多
Evening l夕情丶
3楼-- · 2019-06-22 14:38

Dynamic ui-sref is not supported, but I found an advice on using the generation of the link with the href method of the $state service:

<a href="{{ctrl.url(myState, myKey)}}>

url = function(myState, myKey) {
    return $state.href(myState, {myKey: 'my variable type value'});
}
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-22 14:39

I think the simplest solution is

<button ui-sref="myState({mykey:vm.key})"> Link </button>

where vm.key is the value being passed from the controller. This avoids extra javascript as used in the answer for this. Its very similar to the accepted answer but the variable is used into the html tag as is, without creating a handler.

查看更多
Rolldiameter
5楼-- · 2019-06-22 14:41

Actually I found a solution, so hopefully it will still help someone.

<li ng-repeat="item in array" ui-sref="stateName({param:{{item.key}}})">
     Some content
</li>

In my case I am using ui-router 1.0.15, so not sure if it will work with something older than v1.0.x

查看更多
爷、活的狠高调
6楼-- · 2019-06-22 14:42

document({ id: document.id})

If your state is document/:id

查看更多
太酷不给撩
7楼-- · 2019-06-22 14:54

How to pass parameters using ui-sref in ui-router to controller

Similar question was asked here. His solution was to set up a state that does the routing and you pass params to that state through ui-sref that triggers the desired URL

查看更多
登录 后发表回答