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

ski*_*kip 364 html javascript angularjs angular-ui angular-ui-router

我需要传递并接收两个参数到我想转移到使用ui-srefui-router的状态.

比如使用以下链接将状态转换为homewith foobar参数:

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

控制器中的接收foobar值:

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

我在控制器中得到undefined$stateParam.

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

编辑:

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

    },

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

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

});
Run Code Online (Sandbox Code Playgroud)

Rad*_*ler 530

我已经创建了一个示例来说明如何.更新的state定义是:

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

        },
        ...
      }
Run Code Online (Sandbox Code Playgroud)

这将是控制器:

.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; 
})
Run Code Online (Sandbox Code Playgroud)

我们可以看到,州家现在已将网址定义为:

url: '/:foo?bar',
Run Code Online (Sandbox Code Playgroud)

这意味着,网址中的参数预计为

/fooVal?bar=barValue
Run Code Online (Sandbox Code Playgroud)

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

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

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

链接到doc:

你可以在这里查看

params : {}

还有新的,更精细的设置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',
      },
    ...
Run Code Online (Sandbox Code Playgroud)

可用于params的设置在$ stateProvider的文档中描述

以下只是摘录

  • value - {object | function =}:指定此参数的默认值.这隐式地将此参数设置为可选...
  • array - {boolean =} :( default:false)如果为true,则param值将被视为值数组.
  • squash - {bool | string =}: squash配置当前参数值与默认值相同时,URL中如何表示默认参数值.

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

// 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]})">
Run Code Online (Sandbox Code Playgroud)

这里检查它


Pau*_*gel 107

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

例如,用:

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

    },
  },
  params: {
    foo: null,
    bar: null
  }
})
Run Code Online (Sandbox Code Playgroud)

您可以使用以下任一方法将参数发送到州:

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

当然,如果您在home状态上重新加载一次页面,您将丢失状态参数,因为它们不会存储在任何位置.

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


fel*_*ker 29

你只是错误拼写$stateParam,它应该是$stateParams(带有s).这就是你未定义的原因;)

  • 一般来说,这应该作为问题的评论.但无论如何+1为阅读问题和理解问题所做的努力:) (8认同)