当前位置: 移动技术网 > IT编程>开发语言>JavaScript > AngularJs 禁止模板缓存的方法

AngularJs 禁止模板缓存的方法

2017年12月08日  | 移动技术网IT编程  | 我要评论
本文介绍了angularjs 禁止模板缓存的方法,分享给大家,也给自己留个笔记,具有如下: 因为angularjs的特性(or 浏览器本身的缓存?),angular默认的

本文介绍了angularjs 禁止模板缓存的方法,分享给大家,也给自己留个笔记,具有如下:

因为angularjs的特性(or 浏览器本身的缓存?),angular默认的html模板加载都会被缓存起来。导致每次修改完模板之后都得经常需要清除浏览器的缓存来保证浏览器去获得最新的html模板,自己测试还好,但如果更新了服务器的模板内容,用户可不会每个都配合你去清除浏览器的缓存。故这还真是个大问题。

app.config(function($routeprovider, $locationprovider) { 
 $routeprovider 
  .when('/book/:bookid/ch/', { 
  templateurl: 'chapter.html', 
  controller: 'chaptercontroller' 
 }); 
}); 

方法一:在模板文件路径后加时间戳(or 其他随机数),强制angularjs每次从服务器加载新的模板

app.config(function($routeprovider, $locationprovider) { 
 $routeprovider 
  .when('/book/:bookid/ch/', { 
  templateurl: 'chapter.html' + '?datestamp=' + (new date()).gettime(), 
  controller: 'chaptercontroller' 
 }); 
}); 

不过这种方法太不美观了。。。。

方法二:使用$templatecache清除缓存

// 禁止模板缓存 
app.run(function($rootscope, $templatecache) { 
  $rootscope.$on('$routechangestart', function(event, next, current) { 
    if (typeof(current) !== 'undefined'){ 
      $templatecache.remove(current.templateurl); 
    } 
  }); 
}); 

在配置 路由地址后,即在app.config之后添加这段代码,可禁止angularjs将templateurl缓存起来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网