• <tfoot id='P2Y51'></tfoot>
        <bdo id='P2Y51'></bdo><ul id='P2Y51'></ul>
    1. <small id='P2Y51'></small><noframes id='P2Y51'>

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

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

        AngularUI路由器:具有相同url模式的多个状态

        AngularUI Router: multiple states with same url pattern(AngularUI路由器:具有相同url模式的多个状态)
            • <bdo id='a9lv3'></bdo><ul id='a9lv3'></ul>
                <tbody id='a9lv3'></tbody>

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

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

                <i id='a9lv3'><tr id='a9lv3'><dt id='a9lv3'><q id='a9lv3'><span id='a9lv3'><b id='a9lv3'><form id='a9lv3'><ins id='a9lv3'></ins><ul id='a9lv3'></ul><sub id='a9lv3'></sub></form><legend id='a9lv3'></legend><bdo id='a9lv3'><pre id='a9lv3'><center id='a9lv3'></center></pre></bdo></b><th id='a9lv3'></th></span></q></dt></tr></i><div id='a9lv3'><tfoot id='a9lv3'></tfoot><dl id='a9lv3'><fieldset id='a9lv3'></fieldset></dl></div>
                • <tfoot id='a9lv3'></tfoot>
                • 本文介绍了AngularUI路由器:具有相同url模式的多个状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  大家好,我遇到了我认为常见的路由问题,但我无法找到解决方案.基本上我的页面有两种状态,基本和高级,我希望两种状态的 URL 模式相同,但只加载当前状态的模板(从控制器内部转换)

                  Hey all I'm running into what I thought would be a common routing problem, but I'm unable to figure out a solution. Basically my page has two states, basic and advanced, and I want the URL patterns to be the same for both states but only load the template for the current state at the time (which is transitioned to from within a controller)

                  config(function ($stateProvider) {
                  
                    $stateProvider.state('basic', {
                      url: '/:post',
                      templateUrl: function (stateParams) {
                        return 'post-' + stateParams.post + '-tmpl.html';
                      }
                    });
                  
                    $stateProvider.state('advanced', {
                      url: '/:post',
                      templateUrl: function (stateParams) {
                        return 'post-' + stateParams.post + '-advanced-tmpl.html';
                      }
                    });
                  })
                  
                  controller('myCtrl', function ($state) {
                    //
                    // In this case, I would expect only the template from
                    // the advanced state to load, but both templates are trying
                    // to load.
                    $state.transitionTo('advanced', {post: 2});
                  }
                  

                  我假设导航到匹配的模式会加载给定的状态,这就是为什么当它匹配时,两个模板都会尝试加载.有什么方法可以实现相同的 url 模式,但仅基于当前状态使用不同的模板?

                  I assume that navigating to the matched pattern loads the given state which is why when it matches, both templates attempt to load. Is there some way to accomplish the same url pattern but with different templates based only on the current state?

                  推荐答案

                  假设你不能有两个具有相同 URL 的状态,为什么不继续将两个状态合并为一个呢?ui-router 已经允许你有一个自定义函数来返回模板.您只需在状态声明中添加另一个隐藏的自定义参数(我们称之为 advanced):

                  Assuming that you cannot have two states with the same URL, why don't you go along and merge the two states into one? Ui-router already allows you to have a custom function to return the template. You just need to add another, hidden custom parameter (let's call it advanced) to the state declaration:

                  $stateProvider.state('basicOrAdvanced', {
                    url: '/:post',
                    templateUrl: function (stateParams) {
                      if (stateParams.advanced) {
                        return 'post-' + stateParams.post + '-advanced-tmpl.html';
                      } else {
                        return 'post-' + stateParams.post + '-tmpl.html';
                      }
                    },
                    params: {
                      advanced: False
                    }
                  });
                  

                  然后你调用它:

                  $state.transitionTo('basicOrAdvanced', {post: 2, advanced: True})
                  

                  <小时>

                  有关嵌套状态和通用控制器的另一种可能解决方案,请参阅问题 #217 和 #1096 在 ui-router 的 github 页面上.


                  For another possible solution with nested states and a common controller, see issues #217 and #1096 on ui-router's github page.

                  那里提出的解决方案创建了第三种状态,其控制器执行调度工作,而您想要进入的两种状态(basicadvanced)的 URL 为空:

                  The solution presented there creates a third state whose controller does the dispatching work, whereas the two states you want to land in (basic and advanced) have empty URLs:

                  $stateProvider.state('basicOrAdvanced', {
                    url: '/:post',
                    controller: function($state, $stateParams) {
                      if($stateParams.advanced) {
                          $state.go('advanced', {post: $stateParams.post});
                      } else {
                          $state.go('basic', {post: $stateParams.post});
                      }
                    },
                    params: {
                      advanced: False
                    }
                  }
                  

                  如果您的状态与简单的 templateUrl 相比在更多方面有所不同(例如,如果它们具有完全独立的控制器等),则此解决方案会更好

                  This solution is better if your states differ in more aspects than the simple templateUrl (e.g. if they have completely separate controllers, etc.)

                  另请参阅 另一个关于 StackOverflow 的关于类似问题的问题.

                  这篇关于AngularUI路由器:具有相同url模式的多个状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)
                    <tbody id='dmKY0'></tbody>

                  <tfoot id='dmKY0'></tfoot>

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

                    <legend id='dmKY0'><style id='dmKY0'><dir id='dmKY0'><q id='dmKY0'></q></dir></style></legend>
                    <i id='dmKY0'><tr id='dmKY0'><dt id='dmKY0'><q id='dmKY0'><span id='dmKY0'><b id='dmKY0'><form id='dmKY0'><ins id='dmKY0'></ins><ul id='dmKY0'></ul><sub id='dmKY0'></sub></form><legend id='dmKY0'></legend><bdo id='dmKY0'><pre id='dmKY0'><center id='dmKY0'></center></pre></bdo></b><th id='dmKY0'></th></span></q></dt></tr></i><div id='dmKY0'><tfoot id='dmKY0'></tfoot><dl id='dmKY0'><fieldset id='dmKY0'></fieldset></dl></div>

                      • <bdo id='dmKY0'></bdo><ul id='dmKY0'></ul>