[英]Loading partial with Angular directives not rendering the second time the view is loaded
[英]Angular Script Loading inside partial view - caching issues
我在Angular应用程序的部分视图中加载JavaScript时遇到问题。
这是我的设置: ui-router
将对/profile
请求的ui-view
设置为<app-home-profile></app-home-profile>
。
appHomeProfile
指令设置如下:
app.directive('appHomeProfile', function() {
return {
restrict: 'E',
templateUrl: 'templates/user/profile.html',
controller: ...,
controllerAs: 'profileCtrl'
}
});
现在,在profile.html
文件中,我包括标准HTML,并在其底部包含一些JavaScript依赖项:
<script type="text/javascript" src="/javascripts/myScript1.js"></script>
<script type="text/javascript" src="/javascripts/myScript2.js"></script>
问题在于服务器日志显示Angular将时间戳添加到请求中,因此不允许缓存任何脚本! 如果脚本以“常规”方式加载到文档的head
,则不会发生这种情况。
GET /javascripts/myScript1.js?_=1437207405398 200 2.356 ms - 22524
GET /javascripts/myScript2.js?_=1437207405399 200 1.873 ms - 29695
感谢您提供有关如何解决此问题的帮助!
谢谢,尼克
与jQuery相反,由于jqLite简化了实现,因此Angular在请求的模板中不支持<script>
。 它可以是固定的那样 。
它在您的示例中起作用的原因是因为您在应用中使用jQuery,因此它管理DOM而不是jqLite。 添加了AJAX请求中的时间戳以禁用浏览器缓存,这是jQuery的默认行为。 可以通过以下方式禁用
app.config(function () {
angular.element.ajaxSetup && angular.element.ajaxSetup({ cache: true });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.