【问题标题】:How to pass parameters using ui-sref in ui-router to controller如何在ui-router中使用ui-sref将参数传递给控制器
【发布时间】:2014-10-28 03:20:23
【问题描述】:

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

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

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

在控制器中接收 foobar 值:

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

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

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

编辑:

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

    },

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

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

});

【问题讨论】:

  • 您还需要在您的路线中设置它们。例如:- url:'.../home/:foo/:bar
  • @PSL:你能在这里举个小例子吗?谢谢。
  • @PSL:谢谢 PSL,有没有其他方法可以让我在控制器中获得这些参数及其值?
  • 在你的空白路由中做'/:foo/:bar': {,或者你也可以设置为查询字符串。

标签: javascript html angularjs angular-ui-router angular-ui


【解决方案1】:

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

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

        },
        ...
      }

这就是控制器:

.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; 
})

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

url: '/:foo?bar',

这意味着 url 中的参数应为

/fooVal?bar=barValue

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

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

此外,控制器确实使用 $stateParams 而不是 $stateParam

文档链接:

您可以查看here

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',
      },
    ...

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

以下只是摘录

  • value - {object|function=}:指定此参数的默认值。这隐式将此参数设置为可选...
  • array - {boolean=}:(默认值:false)如果为 true,则参数值将被视为值数组。
  • 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]})">

在行动中检查它here

【讨论】:

【解决方案2】:

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

例如,用:

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

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

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

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

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

此行为的完整描述记录在here,在state(name, stateConfig) 部分的params 行下。

【讨论】:

  • params 属性对我来说非常好用!!!!!!谢谢!!你是谷歌专家
【解决方案3】:

你只是拼错了$stateParam,它应该是$stateParams(带有一个s)。这就是你不确定的原因;)

【讨论】:

  • 通常这应该作为问题中的评论。但无论如何 +1 为阅读问题和理解问题所付出的努力:)
猜你喜欢
  • 2018-12-20
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多