在angular-ui-router中从ui-sref更改当前状态的url参数

时间:2022-10-29 19:40:22

Using ui-router I would like to set a URL parameter no matter what state I'm in. A use-case for this is to switch between bookmarkable interface modes (e.g. "boring" or "funny").

使用ui-router我想设置一个URL参数,无论我处于什么状态。用例是在可收藏的界面模式之间切换(例如“无聊”或“有趣”)。

I know it can be done using ng-click directive, but in this case I'm left with href-less anchors, which is not very useful.

我知道它可以使用ng-click指令完成,但在这种情况下我留下了href-less锚点,这不是很有用。

I have tried the most obvious ui-sref=".({foo:'bar'})" but it fails with "No reference point given for path '.'"

我已经尝试过最明显的ui-sref =“。({foo:'bar'})”但它失败了,“没有为路径提供参考点。”

Does ui-router have a native solution to do this? Ideally I would like to have something like:

ui-router是否有本机解决方案来执行此操作?理想情况下,我希望有类似的东西:

<a ui-sref="currentState({interface:'boring'})">boring</a>
<a ui-sref="currentState({interface:'fun'})">fun</a>

3 个解决方案

#1


11  

It seems that this functionality is available in the yet unreleased master branch of ui-router:

似乎这个功能在ui-router的未发布的主分支中可用:

<a ui-sref="{foo:'bar'}">foobar</a>

See the related issue: https://github.com/angular-ui/ui-router/issues/1031

请参阅相关问题:https://github.com/angular-ui/ui-router/issues/1031

#2


5  

Or for the time being you can do something like this in the controller or in app run as I prefer to do it so it's available everywhere (each view):

或者暂时你可以在控制器或应用程序中执行类似的操作,因为我喜欢这样做,因此它随处可用(每个视图):

appModule.run(function($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from) {
            $rootScope.previousState = from.name;
            $rootScope.currentState = to.name;
        });
    });

Then should then be able to use:

那么应该可以使用:

<a ui-sref="{{currentState}}({interface:'boring'})">boring</a>

#3


2  

I know this is old topic but

我知道这是老话题但是

ui-sref="."

should work.

https://github.com/angular-ui/ui-router/pull/2541

#1


11  

It seems that this functionality is available in the yet unreleased master branch of ui-router:

似乎这个功能在ui-router的未发布的主分支中可用:

<a ui-sref="{foo:'bar'}">foobar</a>

See the related issue: https://github.com/angular-ui/ui-router/issues/1031

请参阅相关问题:https://github.com/angular-ui/ui-router/issues/1031

#2


5  

Or for the time being you can do something like this in the controller or in app run as I prefer to do it so it's available everywhere (each view):

或者暂时你可以在控制器或应用程序中执行类似的操作,因为我喜欢这样做,因此它随处可用(每个视图):

appModule.run(function($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from) {
            $rootScope.previousState = from.name;
            $rootScope.currentState = to.name;
        });
    });

Then should then be able to use:

那么应该可以使用:

<a ui-sref="{{currentState}}({interface:'boring'})">boring</a>

#3


2  

I know this is old topic but

我知道这是老话题但是

ui-sref="."

should work.

https://github.com/angular-ui/ui-router/pull/2541