我在Angular应用程序的部分视图中加载JavaScript时遇到问题。
这是我的设置:ui-router
将ui-view
for请求设置/profile
为<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相反,<script>
由于jqLite简化了实现,因此Angular不支持所请求的模板。它可以是固定的那样。
它在您的示例中起作用的原因是因为您在应用程序中使用jQuery,因此它管理DOM而不是jqLite。添加了AJAX请求中的时间戳以禁用浏览器缓存,这是jQuery的默认行为。可以通过以下方式禁用
app.config(function () {
angular.element.ajaxSetup && angular.element.ajaxSetup({ cache: true });
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句