angularJs $templateCache

  模板加载后,AngularJS会将它默认缓存到 $templateCache 服务中。在实际生产中,可以提前将模板缓存到一个定义模板的JavaScript文件中,这样就不需要通过XHR来加载模板了

  $templateCache 服务允许 $http 服务缓存经过XHR的模板请求,这样它们就只会被请求一次。当一个模板被取到了,它的内容就会存储在 $templateCache 中,用模板路径作键。例如,当获取下面的实例指令时,它会请求 templateUrl 并且把模板的内容放在 $templateCache 中:

1 angular.module(‘myApp‘)
2     .directive(‘notification‘, function($timeout) {
3         return {
4             restrict: ‘A‘,
5             scope: { ngModel: ‘=‘ },
6             templateUrl: ‘views/templates/notification.html‘,
7 }});

$templateCache 会 把 这 个 模 板 的 内 容 保 持 在 $templateCache(‘views/templates/notification.html‘) 中。如果已经预先在 $templateCache 中存放了测试所需的指令文件内容,就可以使用 $templateCache 来阻止在指令的单元测试中再产生请求。可以使用优秀的 karma-ng-html2js-preprocessor 包来把模板转换成可在测试中使用的Angular模块。

利用 $templateCache
  在生产中部署应用时,我们都希望应用的加载尽可能快,以及尽可能做出响应。使用XHR加载模板可能会导致Web应用缓慢或者有卡顿的感觉。可以通过将模板包装为JavaScript文件,然后连同应用程序的其他部分一起传输的方式伪造模板缓存加载,而不是通过XHR提取模板。关于如何有效地包装模板的详细信息,请参考 $templateCache 工具: grunt-angular-templates 。

  默认情况下,Angular无法从本地 $tempalteCache 中找到模板时,会通过XHR提取模板。当XHR请求很慢,或者模板很大时,它可能会对应用的用户体验造成很大的负面影响。
  你可以通过“伪造” $templateCache 已经被填充的方式来避免这一延迟,这样Angular就不必从远程加载模板。可以在JavaScript中手动实现这个技巧,就像这样:

1 angular.module(‘myApp‘,[])
2     .run(function($templateCache) {
3         $templateCache.put(‘home.html‘, ‘This is the home template‘);
4 });

  现在,当Angular需要提取名为home.html的模板时,它会在 $templateCahce 中找到它,而无需从服务器提取。

时间: 2024-12-28 18:26:23

angularJs $templateCache的相关文章

Scope in AngularJS

From the RUNBOOM.COM, it said: Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Scope 可应用在视图和控制器上. While, how many scopes can we have? is there any structure in it? I got those question when I come to it. After a tough time r

野兽的Angular Api 学习、翻译及理解 - - $templateCache 和 $templateRequest

野兽的ng api学习 -- $templateCache 和 $templateRequest $templateCache 第一次使用模板,它被加载到模板缓存中,以便快速检索.你可以直接将模板标签加载到缓存中,或者通过$templateCache服务. 通过script标签: <script type=”text/ng-template” id=”template.html”> <p>This is the content of the template</p> &

学会使用AngularJS

这是用前端框架AngularJS构建一个简单应用的部分代码,首页配制一个路由.当然,AngularJS路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样.前端路由需要提前对指定 的(ng-app),定义路由规则(routeProvider),然后通过不同的URL,告诉(ng-app)加载哪个页面(HTML),再渲染到(ng-app)视图(ng-view)中.虽然URL输入不一样,页面展示不一样,其实完成的是单页(ng-app)视图(ng-view)的局部刷新. 由上图可以知道: 控制器

[AngularJS] Taking control of your templates using $templateCache

Using $templateCache for quickly retrieval from the cache after first time used. $templateCache mainly can use two methods: get(id) put(id, "your html code here") angular.module('app', []) .directive('myButton', function () { return { templateUr

另类angularjs应用

回顾 上一篇文章主要讲解了创建兼容任意浏览器(主要是ie的一些奇葩问题)的angularjs web应用,但是项目开发中其实更重要的还是在功能的模块化.代码自动压缩上面,这样项目在后期维护或者功能的重复利用上才会更方便,那么今天主要围绕以下几个主题来讲讲如何在不是用其他js模块化库的情况下,开发便于管理的angualrjs模块化代码: 使用service来创建模块 模块间引用 代码合并及压缩 本文中的代码均已nodejs来实现. nodejs中,使用UglifyJS2来实现代码压缩 使用serv

Getting Started with Django Rest Framework and AngularJS

转载自:http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html A ReSTful API is becoming a standard component of any modern web application. The Django Rest Framework is powerful framework for developing ReST endpoints

angularjs

Angular数据绑定的核心:脏检查(通过事件循环检查数据模型的变化) 双向数据绑定:意味着如果视图改变了某个值,数据模型会通过脏检查观察到这个变化,而如果数据模型改变了某个值,视图也会依据变化重新渲染. 模块: 模块声明: angular.module('myApp', ['m1']); 第一个参数是模块名称,第二个参数是依赖的模块名称. 模块获取: angular.module('myApp') scope:$scope对象是定义应用业务逻辑.控制器和视图属性的地方.AngularJS启动并

表单验证&amp;lt;AngularJs&amp;gt;

经常使用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,仅仅要在输入字段元素上加入HTML5标记required就可以: <input type="text" required />   2. 最小长度 验证表单输入的文本长度是否大于某个最小值,在输入字段上使用指令ng-minleng= "{number}": <input type="text" ng-minlength="5" /> 3.

初识AngularJs

今天看看angular.文章将从三个大的部分来说,第一部分什么是angular,第二部分,初次使用时需要注意的一些事项,第三部分,angular基础概念: 一,什么是AngularJs? AngularJs是为了克服html在构建应用上的不足而设计的,它是一个JavaScript框架.通过使用我们称为标识符的结构,让浏览器能够识别新的语法.例如使用{{}}进行数据绑定. AngularJs通过为开发者呈现一个更高层次的抽象来简化应用开发,这在带来方便的同时也导致灵活性的降低.AngularJs主