<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将参数传递给控制器

        时间:2023-09-30
          <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将参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何让 Plotly.js 监听刻度标签的点击事件? 下一篇:Angular UI-Router $urlRouterProvider .当我单击 &lt;a ui-sref

                  相关文章

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