热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在ui-路由器的模板中使用$templateCache。-Using$templateCacheinui-router'stemplate

Caniuse$templateCacheinui-routerstemplate?我可以在ui-router的模板中使用$templateCache吗?Thetemplate

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

我可以在ui-router的模板中使用$templateCache吗?

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

该模板将缓存在resolve部分,我希望在相同的状态下使用缓存模板。

$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"

Update: (+ Code updated)

更新:(+代码更新)

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

我认为我的代码的解析部分有一个问题。因为它运行在模板部分之后!它导致返回$templateCache。得到被定义。

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

我使用ocLazyLoad插件缓存模板,它返回一个正确的承诺。

Why template don't waits for resolve?

为什么模板不等待解决?

3 个解决方案

#1


19  

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:

如何动态地设置动态模板不是通过模板属性,而是通过templateProvider。有一个工作的活塞,这是它的代码片段:

// 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'
    , '

dashboard

'); }) .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'); }, }) });

See:

看到的:

  • Templates
  • 模板

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,它已经被ui-router使用了。负责为我们的视图加载模板(从url、string…)的主要服务是:

  • $templateFactory
  • templateFactory美元

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

正如其代码所示,它使用$templateCache作为自然优化($templateFactory代码片段:)

...
/**
* @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.} 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; });
};
...

#2


3  

Well, I just found out that you can simply set the key of your $templateCache template as the templateUrl route property and it works fine.

我刚刚发现,只需将$templateCache模板的键设置为templateUrl路径属性,它就可以正常工作。

#3


0  

I do not know if it has already been solved, however, I decided to update my template as follows. I remember that I use AngularAMD / AngularUIRouter, but the templateURL works the same way. I use the function in templateURL, where I pass the "xxx" parameter equal to a new date every time, and this forces the page search again. I hope I have helped okay.

我不知道是否已经解决,但是我决定更新我的模板如下。我记得我使用了AngularAMD / AngularUIRouter,但是templateURL的工作方式是一样的。我在templateURL中使用这个函数,每次都传递“xxx”参数,等于一个新的日期,这将再次强制页面搜索。我希望我帮了忙。

.state ('MyReports', {
    parent: 'index',
    url: '/MyReports/:cd_menu',
    views: {
        'BodyView': angularAMD.route ({templateUrl: function (params) {
        return "MyReportsAction/getTemplate?cd_menu="+ params.cd_menu + "&xxx="+ new Date();
    },
    controllerUrl: 'js/app/base/controllers/MyReportsController',
    controller: 'MyReportsController'})
}
})

Once this is done, the code below works as expected:

一旦这样做了,下面的代码将按预期工作:

$state.go('MyReports', {'cd_menu': cd_menu});

推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • JVM 学习总结(三)——对象存活判定算法的两种实现
    本文介绍了垃圾收集器在回收堆内存前确定对象存活的两种算法:引用计数算法和可达性分析算法。引用计数算法通过计数器判定对象是否存活,虽然简单高效,但无法解决循环引用的问题;可达性分析算法通过判断对象是否可达来确定存活对象,是主流的Java虚拟机内存管理算法。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
author-avatar
北斗七星
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有