<legend id='Nf5wF'><style id='Nf5wF'><dir id='Nf5wF'><q id='Nf5wF'></q></dir></style></legend>

      <small id='Nf5wF'></small><noframes id='Nf5wF'>

      1. <tfoot id='Nf5wF'></tfoot>

      2. <i id='Nf5wF'><tr id='Nf5wF'><dt id='Nf5wF'><q id='Nf5wF'><span id='Nf5wF'><b id='Nf5wF'><form id='Nf5wF'><ins id='Nf5wF'></ins><ul id='Nf5wF'></ul><sub id='Nf5wF'></sub></form><legend id='Nf5wF'></legend><bdo id='Nf5wF'><pre id='Nf5wF'><center id='Nf5wF'></center></pre></bdo></b><th id='Nf5wF'></th></span></q></dt></tr></i><div id='Nf5wF'><tfoot id='Nf5wF'></tfoot><dl id='Nf5wF'><fieldset id='Nf5wF'></fieldset></dl></div>
          <bdo id='Nf5wF'></bdo><ul id='Nf5wF'></ul>
      3. 如何使用ui-router中的ui-sref将参数传递给控制器

        How to pass parameters using ui-sref in ui-router to controller(如何使用ui-router中的ui-sref将参数传递给控制器)
          <legend id='tgAjI'><style id='tgAjI'><dir id='tgAjI'><q id='tgAjI'></q></dir></style></legend>

          <small id='tgAjI'></small><noframes id='tgAjI'>

        • <tfoot id='tgAjI'></tfoot>
          • <bdo id='tgAjI'></bdo><ul id='tgAjI'></ul>
                <tbody id='tgAjI'></tbody>
              <i id='tgAjI'><tr id='tgAjI'><dt id='tgAjI'><q id='tgAjI'><span id='tgAjI'><b id='tgAjI'><form id='tgAjI'><ins id='tgAjI'></ins><ul id='tgAjI'></ul><sub id='tgAjI'></sub></form><legend id='tgAjI'></legend><bdo id='tgAjI'><pre id='tgAjI'><center id='tgAjI'></center></pre></bdo></b><th id='tgAjI'></th></span></q></dt></tr></i><div id='tgAjI'><tfoot id='tgAjI'></tfoot><dl id='tgAjI'><fieldset id='tgAjI'></fieldset></dl></div>
                  本文介绍了如何使用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.

                  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>
                  

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

                  I get undefined for $stateParam in the controller.

                  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:

                    $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:

                  url: '/:foo?bar',
                  

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

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

                  Link to doc:

                  • URL Parameters

                  You can check it here

                  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:

                  .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

                  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.

                  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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                    <legend id='vijj9'><style id='vijj9'><dir id='vijj9'><q id='vijj9'></q></dir></style></legend>
                      <tfoot id='vijj9'></tfoot>
                        <bdo id='vijj9'></bdo><ul id='vijj9'></ul>

                          <small id='vijj9'></small><noframes id='vijj9'>

                          1. <i id='vijj9'><tr id='vijj9'><dt id='vijj9'><q id='vijj9'><span id='vijj9'><b id='vijj9'><form id='vijj9'><ins id='vijj9'></ins><ul id='vijj9'></ul><sub id='vijj9'></sub></form><legend id='vijj9'></legend><bdo id='vijj9'><pre id='vijj9'><center id='vijj9'></center></pre></bdo></b><th id='vijj9'></th></span></q></dt></tr></i><div id='vijj9'><tfoot id='vijj9'></tfoot><dl id='vijj9'><fieldset id='vijj9'></fieldset></dl></div>
                              <tbody id='vijj9'></tbody>