ui-router:templateUrl 函数中的异步数据 [英] ui-router: async data in templateUrl function

查看:28
本文介绍了ui-router:templateUrl 函数中的异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 ui 路由器的一个新问题.我基本上是在尝试查询我的 API 以获取模板 url,该模板 url 以 Resource 对象的形式存储和返回.问题是 templateUrl 函数似乎以未定义的形式返回.我的代码如下:

I've run into quite a novel problem with ui router. I'm basically trying to query my API for a template url, which is stored and returned as a Resource object. The problem, is it seems that the templateUrl function is returning as undefined. My code is as follows:

(function() {
    'use strict';

  angular.module('myApp')
    .config(function ($stateProvider, PageProvider, $httpProvider) {
      $stateProvider
        .state('core', {
          abstract: true,
          templateUrl: 'app/core/core.index.html',
          controller: 'CoreController',
          controllerAs: 'vm'
        })
        /* Parent page view */
        .state('core.page', {
          url: '/:slug',
          views: {
            'page_content@core': {
              templateUrl: function(params) {
                var slug = params.slug;
                // only need to load core template
                var url = 'assets/templates/' + slug + '.html';

                return url;
              },
              controller: 'PageController',
              controllerAs: 'vm'
            }
          }
        })
        .state('core.page.child', {
          url: '/:child',
          views: {
            'page_content@core': {
              templateUrl: function($stateParams) {
                PageProvider
                  .$get() // $get() returns Page service
                  .findOne($stateParams.child)
                  .$promise
                  .then(function(data){
                    return data.template.url;
                  });
              }
            }
          }
        });
      });
})();

我为我的状态core.page.child"设置了一个断点,以查看作用域中有哪些变量可用,但我发现了一些很奇怪的东西:

I set a breakpoint for my state, 'core.page.child', to see what variables were available in the scope, and I found something quite strange:

我真的不明白为什么会发生这种情况,因为 .then(cb) 仅在承诺解决后才被调用,这意味着它应该按预期返回正确的值 - 但它没有't.

I really don't understand why this is happening, since .then(cb) is only called AFTER the promise is resolved, which means it should return the correct value as expected - but it doesn't.

任何帮助将不胜感激.

我应该补充一点,正在发生的事情是 ui-router 根本没有加载我的模板 - 我得到一个空的 ui-view.我最初认为这可能是我的 $stateProvider 配置有问题,但似乎并非如此.

I should add that what is happening is that ui-router is simply not loading my template at all - I get an empty ui-view. I had initially thought it might be a problem with my $stateProvider configuration, but that doesn't seem to be the case.

Edit2:我根据调用堆栈深入研究了源代码.看来你们俩都是正确的 - ui-router 不支持 promise.

I dug a little into the source code based on the call stack. It seems that both of you are correct - ui-router does NOT work with promises.

/**
 * @ngdoc function
 * @name ui.router.util.$templateFactory#fromConfig
 * @methodOf ui.router.util.$templateFactory
 *
 * @description
 * Creates a template from a configuration object. 
 *
 * @param {object} config Configuration object for which to load a template. 
 * The following properties are search in the specified order, and the first one 
 * that is defined is used to create the template:
 *
 * @param {string|object} config.template html string template or function to 
 * load via {@link ui.router.util.$templateFactory#fromString fromString}.
 * @param {string|object} config.templateUrl url to load or a function returning 
 * the url to load via {@link ui.router.util.$templateFactory#fromUrl fromUrl}.
 * @param {Function} config.templateProvider function to invoke via 
 * {@link ui.router.util.$templateFactory#fromProvider fromProvider}.
 * @param {object} params  Parameters to pass to the template function.
 * @param {object} locals Locals to pass to `invoke` if the template is loaded 
 * via a `templateProvider`. Defaults to `{ params: params }`.
 *
 * @return {string|object}  The template html as a string, or a promise for 
 * that string,or `null` if no template is configured.
 */
this.fromConfig = function (config, params, locals) {
  return (
    isDefined(config.template) ? this.fromString(config.template, params) :
    isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) :
    isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) :
    null
  );
};

看来我要么必须使用 $stateProvider.decorator,要么破解 ui-router 的核心.我想我可能只做后者并提交 PR.我也会将此问题发布到 github 存储库上,看看 ui-router 团队中是否有人对此问题有解决方案.

It seems that I'll either have to use $stateProvider.decorator, or hack ui-router's core. I think I might just do the latter and submit a PR. I'll be posting this issue on the github repo as well, to see if anyone on the ui-router team has a solution for this problem.

推荐答案

Working plunk

您可以使用 resolvetemplateProvider 来做到这一点.

you can use a resolve and templateProvider to do this.

  .state('child', {
      url: '/:child',
      resolve: {
        childTemplate: function ($stateParams, $templateRequest) {
          return $templateRequest($stateParams.child + ".html");
        }
      },
      templateProvider: function(childTemplate) { 
        return childTemplate;
      }
  });

这会创建一个解析,它使用 $stateParams 来获取模板(我正在使用 $templateRequest 来获取,添加到 $templateCache代码>,并将内容全部返回).然后,解析的模板内容被注入到视图的 templateProvider 中.

This creates a resolve which uses the $stateParams to fetch the template (I'm using the $templateRequest to fetch, add to $templateCache, and return the contents all in one). Then, the resolved template content is injected into the view's templateProvider.

这篇关于ui-router:templateUrl 函数中的异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆