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

  • <tfoot id='LkrRF'></tfoot>

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

        基于 API Ajax 调用的 slug 的 Angular UI-Router 动态路由.基于 slug 加载视图

        Angular UI-Router dynamic routing based on slug from API Ajax Call. Load view based on slug(基于 API Ajax 调用的 slug 的 Angular UI-Router 动态路由.基于 slug 加载视图)
        • <bdo id='afP1W'></bdo><ul id='afP1W'></ul>
            • <tfoot id='afP1W'></tfoot>

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

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

                  <tbody id='afP1W'></tbody>

              1. <legend id='afP1W'><style id='afP1W'><dir id='afP1W'><q id='afP1W'></q></dir></style></legend>

                  本文介绍了基于 API Ajax 调用的 slug 的 Angular UI-Router 动态路由.基于 slug 加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可通过 API 访问的服务器数据库中的示例 slug:

                  Examples slugs in server database accessible through API:

                  {slug: "john-smith",type: "user"}
                  {slug: "microsoft-technologies",type: "company"}
                  

                  场景 1: 用户视图 &控制器:http://localhost/john-smith

                  scenario 1 : user view & controller : http://localhost/john-smith

                  .state('user', {
                      url: '/:user',
                      templateUrl: 'partial-user.html',
                      controller: 'userCtrl'
                  })
                  

                  场景 2: 公司观点 &控制器:http://localhost/microsoft-technologies

                  scenario 2 : company view & controller : http://localhost/microsoft-technologies

                  .state('company', {
                      url: '/:company',
                      templateUrl: 'partial-company.html',
                      controller: 'companyCtrl'
                  })
                  

                  现在我想根据从 API 调用到服务器的 slug 创建一个动态状态.

                  Now I want to make make a dynamic state based the slug getting from API Call to the server.

                  我写了一个虚构的代码.但我没有办法实现

                  I written a imaginary code. But I'm not getting way to achieve

                  // Example URL http://localhost/john-smith
                  .state('hybrid', {
                      // /john-smith
                      url: '/:slug',
                      templateUrl: function () {
                          return "partial-"+type+".html"
                      },
                      controllerProvider: function (rt) {
                          return type+'Controller'
                      },
                      resolove: {
                          type: function ($http, $stateParams) {
                              $http.get({
                                  method: "GET",
                                  url: "http://localhost/api/" + $stateParams.slug
                              }).success(function(response, status, headers, config){
                                  //response = {slug: "john-smith",type: "user"}
                                  return response.type
                              })
                              return 
                          }
                      }    
                  })
                  

                  推荐答案

                  有一个 一个工作的 plunker.

                  它来自类似的问题:AngularJS ui-router - 两个相同的路由组

                  如果我确实理解你的目标,这将是调整后的状态定义(我只是跳过了 $http 和服务器响应部分,只是使用传递的参数):

                  In case, I do understand your aim properly, this would be the adjusted state definition (I just skipped the $http and server response part, just working with passed parameter):

                  .state('hybrid', {
                      // /john-smith
                      url: '/{slug:(?:john|user|company)}',
                      templateProvider: ['type', '$templateRequest',
                        function(type, templateRequest) 
                        {
                          var tplName = "tpl.partial-" + type + ".html";
                          return templateRequest(tplName);
                        }
                      ],
                      controllerProvider: ['type',
                        function(type) 
                        {
                          return type + 'Controller';
                        }
                      ],
                      resolve: {
                        type: ['$http', '$stateParams',
                          function($http, $stateParams) {
                            /*$http.get({
                              method: "GET",
                              url: "http://localhost/api/" + $stateParams.slug
                          }).success(function(response, status, headers, config){
                              //response = {slug: "john-smith",type: "user"}
                              return response.type
                          })*/
                            return $stateParams.slug
                          }
                        ]
                      }
                  })
                  

                  一个变化是 resolove : {} 变成了:resolve : {}.另一个是控制器def(rt vs type)的夹具.而且,我们确实从内置功能 templateProvider$templateRequest (类似:Angular ui.router 重新加载父模板提供者)

                  One change is the resolove : {} became: resolve : {}. Another is fixture of the controller def (rt vs type). And also, we do profit from built in features templateProvider and $templateRequest (similar here: Angular ui.router reload parent templateProvider)

                  检查实际情况这里

                  EXTEND,包括 $http 部分(扩展插件)

                  EXTEND, including the $http part (extended plunker)

                  让我们调整(出于 plunker 目的)服务器部分以将此信息返回为 data.json:

                  Let's adjust (for plunker purposes) the server part to return this info as data.json:

                  {
                   "john-smith": {"type": "user"},
                   "lady-ann": {"type": "user"},
                   "microsoft-technologies" : {"type": "company" },
                   "big-company" : {"type": "company" },
                   "default": {"type" : "other" }
                  }
                  

                  还有这些链接:

                  <a href="#/john-smith">
                  <a href="#/lady-ann">
                  
                  <a href="#/microsoft-technologies">
                  <a href="#/big-company">
                  
                  <a href="#/other-unknown">
                  

                  通过这个调整后的状态def可以轻松管理:

                  Will be easily managed by this adjusted state def:

                    .state('hybrid', {
                      // /john-smith
                      url: '/:slug',
                      templateProvider: ['type', '$templateRequest',
                        function(type, templateRequest) 
                        {
                          var tplName = "tpl.partial-" + type + ".html";
                          return templateRequest(tplName);
                        }
                      ],
                      controllerProvider: ['type',
                        function(type) 
                        {
                          return type + 'Controller';
                        }
                      ],
                      resolve: {
                        type: ['$http', '$stateParams',
                          function($http, $stateParams) {
                            return $http.get("data.json")
                              .then(function(response){
                                var theType = response.data[$stateParams.slug]
                                    ||response.data["default"]
                                return theType.type
                              })
                          }
                        ]
                      }
                    })
                  

                  在此处查看更新内容

                  这篇关于基于 API Ajax 调用的 slug 的 Angular UI-Router 动态路由.基于 slug 加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

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

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