简体   繁体   English

从控制器更改路径参数

[英]Change route parameter from controller

I have state defined like this: 我有这样定义的状态:

stateProvider
        .state('app', {
            templateUrl: 'views/layout.html',
            url: '/:lang/app',
            resolve: {
                lang: ['$rootScope', '$stateParams', function(rootScope, stateParams){
                    return stateParams.lang;
                }]
            }
        });

I have buttons defined for languages, which should navigate to exact same url except of lang parameter should be changed: 我为语言定义了按钮,这些按钮应导航到完全相同的url,但应更改lang参数:

<a ui-sref="???({ lang: 'en' })"> English </a>

Is it possible? 可能吗?

I wrote own directive for this purpose: 为此,我编写了自己的指令:

angular.module('app').directive('uiSrefParams', ['$state', function(state){
    return {
        restrict: 'A',
        link: function(scope, element, attrs){
            element.click(function(){
                var params = scope.$eval(attrs['uiSrefParams']);
                state.go(state.$current, params);
            });
        }
    };
}]);

usage: 用法:

<a ui-sref-params="{ lang: 'en' }"> English </a>

I would say, that this feature is already built in the ui-router . 我要说的是,该功能已经内置在ui-router

1) As documented here: 1)如此处记录:

ui-sref='stateName' - Navigate to state, no params. ui-sref='stateName'导航至状态,无参数。 'stateName' can be any valid absolute or relative state , following the same syntax rules as $state.go() 'stateName'可以是任何有效的绝对或相对状态 ,遵循与$state.go()相同的语法规则

2) And the doc here 2)这里的文档

The name of the state that will be transitioned to or a relative state path. 将转换到的状态名称或相对状态路径。 If the path starts with ^ or . 如果路径以^或开头. then it is relative, otherwise it is absolute. 那是相对的,否则是绝对的。

Some examples: 一些例子:

$state.go('contact.detail') will go to the 'contact.detail' state
$state.go('^') will go to a parent state.
$state.go('^.sibling') will go to a sibling state.
$state.go('.child.grandchild') will go to a grandchild state.

3) And finally there is a plunker , showing that this feature could be used out of the box: 3)最后有一个插件 ,表明可以立即使用此功能:

Having 2 states (the above plus its 'app.child') we can use this syntax inside of the 'app' state 具有2个状态(上面加上其“ app.child”),我们可以在“ app”状态内使用此语法

<a ui-sref=".({ lang: 'en' })"> English </a> 
<a ui-sref=".({ lang: 'cz' })"> Czech </a>
<a ui-sref=".child({ lang: 'en' })"> Child English </a> 
<a ui-sref=".child({ lang: 'cz' })"> Child Czech </a>

And this could be used in the child: 这可以在孩子中使用:

<a ui-sref=".({ lang: 'en' })"> Sibling English </a> 
<a ui-sref=".({ lang: 'cz' })"> Sibling Czech </a>

Observe the example to see it in action... 观察示例以了解其运行情况...

EXTEND: Check the note from the ui-sref doc EXTEND:检查ui-sref文档中的注释

A note on relative ui-sref targets: 有关ui-sref目标的说明:

You can also use relative state paths within ui-sref , just like the relative paths passed to state.go() . 您还可以在ui-sref使用相对状态路径,就像传递给state.go()的相对路径一样。 You just need to be aware that the path is relative to the state that the link lives in, in other words the state that loaded the template containing the link. 您只需要知道路径是相对于链接所处的状态的,换句话说,就是加载包含链接的模板的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM