--@angularJS--模板加载之缓存模板demo
2015-05-20 15:20
288 查看
不用缓存模板的写法如下:
1、index.html:
<!DOCTYPE HTML>
<html ng-app="app">
<head>
<title>directive-templateUrl</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../css/bootstrap.css">
<script src="../js/angular.js"></script>
</head>
<body>
<!-- 下面是加载指令模板的总入口文件. -->
<div>
<!-- hello标签就代表了templateUrl中加载的模板碎片代码.注意:这样直接加载ff是没什么问题,chrome因为同源安全策略直接就不显示,解决办法是:用缓存模板(对象$templateCache) -->
<hello></hello>
</div>
</body>
<script src="hello.js"></script>
</html>
2、hello.js:
var myModule = angular.module("app",[]);
myModule.directive('hello',function(){
return {
restrict:'AE',
replace:true,
templateUrl:'helloTemplate.html'//helloTemplate.html文件表示的就是<div>Hi there</div>这一串html模板代码碎片文件,hello标签就代表这个模板
}
});
3、模板文件——helloTemplate.html:
<div>Hi there</div>
[解析]:
加载效果:ff页面显示Hi there.chrome页面空白
上面的注意部分已经说过,因为同源安全策略chrome并不显示模板文件,解决办法就是用缓存模板对象$templateCache来加载,那么我们下面来改造下js文件即可:
2、hello.js:
var myModule = angular.module("app",[]);
myModule.run(function($templateCache){
$templateCache.put("helloTemplate.html","<div>hello everyone.</div>");
});// 向缓存中存入模板文件,并用put()方法向模板文件写入<div>hello everyone.</div>这段代码.注意这段代码会覆盖模板文件原有的代码碎片
myModule.directive('hello',function($templateCache){
return {
restrict:'AE',
replace:true,
template:$templateCache.get("helloTemplate.html")// 用get()方法从缓存模板对象获取模板文件
}
});
[解析]:
加载效果:ff和chrome:页面均显示hello everyone.(原代码<div>Hi there</div>被覆盖)
1、index.html:
<!DOCTYPE HTML>
<html ng-app="app">
<head>
<title>directive-templateUrl</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../css/bootstrap.css">
<script src="../js/angular.js"></script>
</head>
<body>
<!-- 下面是加载指令模板的总入口文件. -->
<div>
<!-- hello标签就代表了templateUrl中加载的模板碎片代码.注意:这样直接加载ff是没什么问题,chrome因为同源安全策略直接就不显示,解决办法是:用缓存模板(对象$templateCache) -->
<hello></hello>
</div>
</body>
<script src="hello.js"></script>
</html>
2、hello.js:
var myModule = angular.module("app",[]);
myModule.directive('hello',function(){
return {
restrict:'AE',
replace:true,
templateUrl:'helloTemplate.html'//helloTemplate.html文件表示的就是<div>Hi there</div>这一串html模板代码碎片文件,hello标签就代表这个模板
}
});
3、模板文件——helloTemplate.html:
<div>Hi there</div>
[解析]:
加载效果:ff页面显示Hi there.chrome页面空白
上面的注意部分已经说过,因为同源安全策略chrome并不显示模板文件,解决办法就是用缓存模板对象$templateCache来加载,那么我们下面来改造下js文件即可:
2、hello.js:
var myModule = angular.module("app",[]);
myModule.run(function($templateCache){
$templateCache.put("helloTemplate.html","<div>hello everyone.</div>");
});// 向缓存中存入模板文件,并用put()方法向模板文件写入<div>hello everyone.</div>这段代码.注意这段代码会覆盖模板文件原有的代码碎片
myModule.directive('hello',function($templateCache){
return {
restrict:'AE',
replace:true,
template:$templateCache.get("helloTemplate.html")// 用get()方法从缓存模板对象获取模板文件
}
});
[解析]:
加载效果:ff和chrome:页面均显示hello everyone.(原代码<div>Hi there</div>被覆盖)
相关文章推荐
- Jquery使用ajax以及angularjs 动态模板加载并进行渲染
- AngularJS提供多种模板加载
- 【FreeMarker】【程序开发】模板加载,模板缓存
- javascript模板库jsrender加载并缓存外部模板文件
- angularjs 动态加载js文件,可在模板
- AngularJS实现根据变量改变动态加载模板的方法
- AngularJS模板加载用法详解
- android app -- Picasso 二级缓存加载图片,可控制加载图片大小(附小demo)
- AngularJs 禁止模板缓存
- Angularjs 异步模块加载项目模板
- Angularjs标签模板加载原理
- 【拿来主义】Angularjs标签模板text/ng-template加载原理
- AngularJs 禁止模板缓存的方法
- 使用 AngularJS 的路由和模板实现单页应用 (Single Page)
- AngularJS初始化静态模板详解
- iOS Cell异步图片加载优化,缓存机制详解。
- Glide 一个专注于平滑滚动的图片加载和缓存库
- AngularJS第一篇 ------------- 我的第一个demo
- angularJs中缓存数据,免去重复发起请求的几种写法
- angularJS与bootstrap结合实现动态加载弹出提示内容