如何在 ui-router 中使用 ui-sref 将参数传递给控制器 [英] How to pass parameters using ui-sref in ui-router to controller

查看:21
本文介绍了如何在 ui-router 中使用 ui-sref 将参数传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Something like using the link below for transitioning the state to home with foo and bar parameters:

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

在控制器中接收 foobar 值:

Receiving foo and bar values in a controller:

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

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

I get undefined for $stateParam in the controller.

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

Could somebody help me understand how to get it done?

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

    },

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

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

});

推荐答案

我创建了一个 示例 显示如何操作.更新的 state 定义为:

I've created an example to show how to. Updated state definition would be:

  $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 定义为:

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

url: '/:foo?bar',

这意味着,url 中的参数应为

which means, that the params in url are expected as

/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'})">

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

Also, the controller does consume $stateParams instead of $stateParam.

文档链接:

您可以在此处

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

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:

.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,则 param 值将被视为一组值.
  • squash - {bool|string=}: 当当前参数值与默认值相同时,squash 配置默认参数值在 URL 中的表示方式.
  • 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.

我们可以这样调用这些参数:

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]})">

在操作中检查它这里

这篇关于如何在 ui-router 中使用 ui-sref 将参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆