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

  • <small id='pU8hl'></small><noframes id='pU8hl'>

      • <bdo id='pU8hl'></bdo><ul id='pU8hl'></ul>
      <tfoot id='pU8hl'></tfoot>

        AngularJS ui-router:如何全局解析所有路由的典型数据?

        时间:2023-10-21

        • <small id='PqMb6'></small><noframes id='PqMb6'>

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

                  本文介绍了AngularJS ui-router:如何全局解析所有路由的典型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 AngularJS 服务,它与服务器通信并返回应用程序不同部分的翻译:

                  I have an AngularJS service which communicates with the server and returns translations of different sections of the application:

                  angular
                       .module('utils')
                       .service('Translations', ['$q','$http',function($q, $http) {
                          translationsService = {
                              get: function(section) {
                                  if (!promise) {
                                      var q = $q.defer();
                                      promise = $http
                                              .get(
                                                  '/api/translations',
                                                  {
                                                      section: section
                                                  })
                                              .success(function(data,status,headers,config) {
                                                  q.resolve(result.data);
                                              })
                                              .error(function(data,status,headers,config){ 
                                                  q.reject(status);
                                              });
                                      return q.promise;
                                  }
                              }
                          };
                  
                          return translationsService;
                      }]);
                  

                  节的名称作为 get 函数的 section 参数传递.

                  The name of the section is passed as the section parameter of the get function.

                  我正在使用 AngularJS ui-router 模块并遵循 此处描述的设计模式

                  I'm using AngularJS ui-router module and following design pattern described here

                  所以我有以下状态配置:

                  So I have the following states config:

                  angular.module('app')
                      .config(['$stateProvider', function($stateProvider) {
                      $stateProvider
                      .state('users', {
                          url: '/users',
                          resolve: {
                              translations: ['Translations',
                                  function(Translations) {
                                      return Translations.get('users');
                                  }
                              ]            
                          },
                          templateUrl: '/app/users/list.html',
                          controller: 'usersController',
                          controllerAs: 'vm'
                      })
                      .state('shifts', {
                          url: '/shifts',
                          resolve: {
                              translations: ['Translations',
                                  function(Translations) {
                                      return Translations.get('shifts');
                                  }
                              ]            
                          },
                          templateUrl: '/app/shifts/list.html',
                          controller: 'shiftsController',
                          controllerAs: 'vm'
                      })
                  

                  这很好用,但您可能注意到我必须在 resolve 参数中明确指定 translations.我认为这还不够好,因为这重复了逻辑.

                  This works fine but as you may notice I have to explicitly specify translations in the resolve parameter. I think that's not good enough as this duplicates the logic.

                  有什么方法可以全局解析翻译并避免代码重复.我的意思是某种中间件.

                  Is there any way to resolve translations globally and avoid the code duplicates. I mean some kind of middleware.

                  我正在考虑监听 $stateChangeStart,然后获取特定于新状态的翻译并将它们绑定到控制器,但我还没有找到方法.

                  I was thinking about listening for the $stateChangeStart, then get translations specific to the new state and bind them to controllers, but I have not found the way to do it.

                  任何建议将不胜感激.

                  重要提示:在我的情况下,解析的 translations object 必须包含翻译数据,而不是 service/factory/whatever.

                  Important note: In my case the resolved translations object must contain the translations data, not service/factory/whatever.

                  亲切的问候.

                  推荐答案

                  虽然这是一个非常古老的问题,但我想发布我现在正在使用的解决方案.希望它会在未来对某人有所帮助.在使用了一些不同的方法后,我想出了一个 John Papa 的美丽 angularjs 模式

                  Though this is a very old question, I'd like to post solution which I'm using now. Hope it will help somebody in the future. After using some different approaches I came up with a beautiful angularjs pattern by John Papa

                  他建议使用特殊服务 routerHelperProvider 并将状态配置为常规 JS 对象.我不会在这里复制粘贴整个提供程序.有关详细信息,请参阅上面的链接.但我将展示我是如何通过该服务解决我的问题的.

                  He suggest using a special service routerHelperProvider and configure states as a regular JS object. I'm not going to copy-paste the entire provider here. See the link above for details. But I'm going to show how I solved my problem by the means of that service.

                  这是该提供者的代码部分,它获取 JS 对象并将其转换为状态配置:

                  Here is the part of code of that provider which takes the JS object and transforms it to the states configuration:

                  function configureStates(states, otherwisePath) {
                      states.forEach(function(state) {
                          $stateProvider.state(state.state, state.config);
                  });
                  

                  我把它改成如下:

                  function configureStates(states, otherwisePath) {
                  
                      states.forEach(function(state) {
                  
                          var resolveAlways = {
                  
                              translations: ['Translations', function(Translations) {
                  
                                  if (state.translationCategory) {
                  
                                      return Translations.get(state.translationCategory);
                  
                                  } else {
                  
                                      return {};
                  
                                  }
                  
                              }],
                  
                          };  
                  
                  
                  
                          state.config.resolve =
                  
                              angular.extend(state.config.resolve || {}, resolveAlways || {});
                  
                  
                  
                          $stateProvider.state(state.state, state.config);
                  
                      }); 
                  
                  }); 
                  

                  我的路由配置对象现在如下所示:

                  And my route configuration object now looks as follows:

                          {
                              state: ‘users’,
                              translationsCategory: ‘users’,
                              config: {
                                  controller: ‘usersController’
                                  controllerAs: ‘vm’,
                                  url: ‘/users’.
                                  templateUrl: ‘users.html'
                              }
                  

                  所以我做了什么:

                  我实现了 resolveAlways 对象,该对象采用自定义 translationsCategory 属性,注入 Translations 服务并解析必要的数据.现在不需要每次都这样做了.

                  I implemented the resolveAlways object which takes the custom translationsCategory property, injects the Translations service and resolves the necessary data. Now no need to do it everytime.

                  这篇关于AngularJS ui-router:如何全局解析所有路由的典型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:AngularJS ui-router $state.go('^') 仅更改地址栏中的 URL,但不加载 下一篇:AngularUI路由器:具有相同url模式的多个状态

                  相关文章

                • <tfoot id='WOpLq'></tfoot>
                • <legend id='WOpLq'><style id='WOpLq'><dir id='WOpLq'><q id='WOpLq'></q></dir></style></legend>

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

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

                    1. <small id='WOpLq'></small><noframes id='WOpLq'>