简体   繁体   English

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

[英]How to pass parameters using ui-sref in ui-router to the controller

I need to pass and receive two parameters to the state I want to transit to using ui-sref of ui-router.我需要将两个参数传递和接收到我想要过渡到使用ui-sref的状态。

Something like using the link below for transitioning the state to home with foo and bar parameters:类似于使用下面的链接使用foobar参数将状态转换为home

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

Receiving foo and bar values in a controller:在控制器中接收foobar值:

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

I get undefined for $stateParam in 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'
    }
  }

});

I've created an example to show how to.我创建了一个示例来展示如何操作。 Updated state definition 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 $stateParams instead of $stateParam .此外,控制器确实使用$stateParams而不是$stateParam

Link to doc:链接到文档:

You can check it here你可以在这里查看

params : {}

There is also new , more granular setting params : {} .还有的、更精细的设置params : {} As we've already seen, we can declare parameters as part of url .正如我们已经看到的,我们可以将参数声明为url的一部分。 But with params : {} configuration - we can extend this definition or even introduce paramters which are not part of the 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. value - {object|function=} :指定此参数的默认值。 This implicitly sets this parameter as optional...这隐式将此参数设置为可选...
  • array - {boolean=}: (default: false) If true, the param value will be treated as an array of values.数组 - {boolean=}:(默认值:false)如果为 true,则参数值将被视为值数组。
  • 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. 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此处查看实际情况

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 home state, you will loose the state parameters, as they are not stored anywhere.当然,如果您在home状态下重新加载页面一次,您将丢失状态参数,因为它们没有存储在任何地方。

A full description of this behavior is documented here , under the params row in the state(name, stateConfig) section. 此处记录了此行为的完整描述,位于state(name, stateConfig)部分的params行下。

You simply misspelled $stateParam , it should be $stateParams (with an s).您只是拼错$stateParam ,它应该是$stateParams (带有 s)。 That's why you get undefined ;)这就是为什么你得到未定义的;)

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

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