[英]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.