在ui路由器的模板中使用$ templateCache [英] Using $templateCache in ui-router's template

查看:114
本文介绍了在ui路由器的模板中使用$ templateCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在ui路由器的模板中使用$ templateCache吗?

Can i use $templateCache in ui-router's template?

该模板将缓存在解决"部分中,我想以相同状态使用缓存的模板.

The template will be cached in resolve section and i want to use cached template in the same state.

$stateProvider
.state('dashboard', {
    url: "/dashboard",
    template: function($templateCache){  
        console.log('test 2');
        return $templateCache.get('templates/template1.html'); // returns undefined
    },
    resolve:{
        baseTemplates: function($ocLazyLoad) {
            // here the template will be cached...
            return $ocLazyLoad.loadTemplateFile(['base/dashboard.html']).then(function(){
                console.log('test 1');
            });
        }
    }
})
// console prints "test 2" before than "test 1"

更新:(+代码已更新)

我认为我的代码的解决"部分有问题.因为它在模板部分之后运行!并导致返回未定义的$ templateCache.get.

I Think resolve section of my code has an issue. because it runs after template section! and it cause returning $templateCache.get being undefined.

我使用ocLazyLoad插件缓存模板,它返回正确的Promise.

I use ocLazyLoad plugin to cache template and it returns a correct promise.

为什么模板不等待解决?

推荐答案

动态设置动态模板的方法不是通过 template 属性,而是通过 templateProvider 强>.有一个有效的 plunker ,这是代码段:

The way how to dynamically set the dynamic template is not via the template property but templateProvider. There is a working plunker, and this is the snippet:

// this is a run event (executed after config in fact)
// in which we do inejct a value into $templateCache
.run(function($templateCache){ 
    // this could be lazy... elswhere
    $templateCache.put('templates/template1.html'
    , '<div><h4>dashboard</h4></div>');
  })
.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/dashboard');

    $stateProvider.state('dashboard', {
      url: '/dashboard', 
      // this is the place where to resolve dynamic template
      templateProvider: function($templateCache){  
        // simplified, expecting that the cache is filled
        // there should be some checking... and async $http loading if not found
        return $templateCache.get('templates/template1.html'); 
      },
    })
});

请参阅:

而且,我想说,您不能自己使用$templateCache,但是ui-router已经使用了它.负责为我们的视图加载模板(从url,字符串...)的主要服务是:

And also, I would say, that not ownly you can use the $templateCache, but it is already used by ui-router. The main service responsible for loading templates (from url, string...) for our views is the:

确实使用$templateCache作为自然优化($templateFactory

which, as its code shows, does use $templateCache as a natural optimization ($templateFactory code snippet:)

...
/**
* @ngdoc function
* @name ui.router.util.$templateFactory#fromUrl
* @methodOf ui.router.util.$templateFactory
*
* @description
* Loads a template from the a URL via `$http` and `$templateCache`.
*
* @param {string|Function} url url of the template to load, or a function
* that returns a url.
* @param {Object} params Parameters to pass to the url function.
* @return {string|Promise.<string>} The template html as a string, or a promise
* for that string.
*/
this.fromUrl = function (url, params) {
    if (isFunction(url)) url = url(params);
    if (url == null) return null;
    else return $http
        .get(url, { cache: $templateCache })
        .then(function(response) { return response.data; });
};
...

这篇关于在ui路由器的模板中使用$ templateCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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