Javascript 如何在 ui-router 中使用 ui-sref 将参数传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25647454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:39:15  来源:igfitidea点击:

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

javascripthtmlangularjsangular-ui-routerangular-ui

提问by skip

I need to pass and recieve two parameters to the state I want to transit to using ui-srefof ui-router.

我需要传递和接收两个参数到我想转换到使用ui-srefui-router 的状态。

Something like using the link below for transitioning the state to homewith fooand barparameters:

类似于使用下面的链接将状态转换为homewithfoobar参数:

<a ui-sref="home({foo: 'fooVal', bar: 'barVal'})">Go to home state with foo and bar parameters </a>

Receiving fooand barvalues in a controller:

在控制器中接收foobar取值:

app.controller('SomeController', function($scope, $stateParam) {
  //..
  var foo = $stateParam.foo; //getting fooVal
  var bar = $stateParam.bar; //getting barVal
  //..
});     

I get undefinedfor $stateParamin the controller.

我在控制器中得到undefined$stateParam

Could somebody help me understand how to get it done?

有人可以帮助我了解如何完成它吗?

Edit:

编辑:

.state('home', {
  url: '/',
  views: {
    '': {
      templateUrl: 'home.html',
      controller: 'MainRootCtrl'

    },

    'A@home': {
      templateUrl: 'a.html',
      controller: 'MainCtrl'
    },

    'B@home': {
      templateUrl: 'b.html',
      controller: 'SomeController'
    }
  }

});

回答by Radim K?hler

I've created an exampleto show how to. Updated statedefinition would be:

我创建了一个示例来展示如何操作。更新的state定义是:

  $stateProvider
    .state('home', {
      url: '/:foo?bar',
      views: {
        '': {
          templateUrl: 'tpl.home.html',
          controller: 'MainRootCtrl'

        },
        ...
      }

And this would be the controller:

这将是控制器:

.controller('MainRootCtrl', function($scope, $state, $stateParams) {
    //..
    var foo = $stateParams.foo; //getting fooVal
    var bar = $stateParams.bar; //getting barVal
    //..
    $scope.state = $state.current
    $scope.params = $stateParams; 
})

What we can see is that the state home now has url defined as:

我们可以看到 state home 现在的 url 定义为:

url: '/:foo?bar',

which means, that the params in url are expected as

这意味着,url 中的参数预期为

/fooVal?bar=barValue

These two links will correctly pass arguments into the controller:

这两个链接将正确地将参数传递给控制器​​:

<a ui-sref="home({foo: 'fooVal1', bar: 'barVal1'})">
<a ui-sref="home({foo: 'fooVal2', bar: 'barVal2'})">

Also, the controller does consume $stateParamsinstead of $stateParam.

此外,控制器确实消耗$stateParams而不是$stateParam.

Link to doc:

链接到文档:

You can check it here

你可以在这里查看

params : {}

params : {}

There is also new, more granular setting params : {}. As we've already seen, we can declare parameters as part of url. But with params : {}configuration - we can extend this definition or even introduce paramters which are not part of the url:

还有新的、更精细的设置params : {}。正如我们已经看到的,我们可以将参数声明为url. 但是通过params : {}配置 - 我们可以扩展这个定义,甚至引入不属于 url 的参数:

.state('other', {
    url: '/other/:foo?bar',
    params: { 
        // here we define default value for foo
        // we also set squash to false, to force injecting
        // even the default value into url
        foo: {
          value: 'defaultValue',
          squash: false,
        },
        // this parameter is now array
        // we can pass more items, and expect them as []
        bar : { 
          array : true,
        },
        // this param is not part of url
        // it could be passed with $state.go or ui-sref 
        hiddenParam: 'YES',
      },
    ...

Settings available for params are described in the documentation of the $stateProvider

$stateProvider的文档中描述了可用于参数的设置

Below is just an extract

以下只是摘录

  • value - {object|function=}: specifies the default value for this parameter. This implicitly sets this parameter as optional...
  • array - {boolean=}:(default: false) If true, the param value will be treated as an array of values.
  • squash - {bool|string=}:squash configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value.
  • value - {object|function=}:指定此参数的默认值。这隐式地将此参数设置为可选...
  • 数组 - {boolean=}:(默认值:false)如果为 true,则参数值将被视为值数组。
  • squash - {bool|string=}:当当前参数值与默认值相同时,squash 配置如何在 URL 中表示默认参数值。

We can call these params this way:

我们可以这样称呼这些参数:

// hidden param cannot be passed via url
<a href="#/other/fooVal?bar=1&amp;bar=2">
// default foo is skipped
<a ui-sref="other({bar: [4,5]})">

Check it in action here

这里检查它的行动

回答by Paul Mougel

You don't necessarily need to have the parameters inside the URL.

您不一定需要在 URL 中包含参数。

For instance, with:

例如,与:

$stateProvider
.state('home', {
  url: '/',
  views: {
    '': {
      templateUrl: 'home.html',
      controller: 'MainRootCtrl'

    },
  },
  params: {
    foo: null,
    bar: null
  }
})

You will be able to send parameters to the state, using either:

您将能够使用以下任一方法将参数发送到状态:

$state.go('home', {foo: true, bar: 1});
// or
<a ui-sref="home({foo: true, bar: 1})">Go!</a>

Of course, if you reload the page once on the homestate, you will loose the state parameters, as they are not stored anywhere.

当然,如果您在home状态上重新加载页面一次,您将丢失状态参数,因为它们没有存储在任何地方。

A full description of this behavior is documented here, under the paramsrow in the state(name, stateConfig)section.

此处记录了此行为的完整描述,params位于state(name, stateConfig)部分中的行下方。

回答by felixfbecker

You simply misspelled $stateParam, it should be $stateParams(with an s). That's why you get undefined ;)

您只是拼错了$stateParam,它应该是$stateParams(带有 s)。这就是为什么你得到 undefined ;)