gpt4 book ai didi

javascript - AngularJS 指令 templateUrl 不更新

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:37 24 4
gpt4 key购买 nike

我对 Angular Directive(指令)有疑问。当我编辑通过 templateUrl 引用的文件的内容时,结果不会出现,直到我删除缓存。我有以下代码:

指令.js

.directive('appMainsec',['$window', function ($window){
var objectMainSec = {
restrict: 'A',
templateUrl: 'partials/app-mainsec.html',
controller: function (){},
controllerAs: 'MainSectCtrl',
link: function ($scope,elemnt,attr){
elemnt.css('height', ($window.innerHeight - ($window.innerHeight * .25)) + 'px');
}
};

return objectMainSec;
}]);

app-mainsec.html

<div><h1>Principal</h1></div>

和index.html

...
<div app-mainsec></div>
...

当我改变 <h1>Hi</h1><h1>Hello</h1> ,指令的 View 在我删除缓存之前不会更新。

最佳答案

原因是 Angular 在开始时只获取一次文件。您可以尝试将 templateUrl 用作函数并附加时间戳,以便每次都能获得新的模板 url。

templateUrl: function() {
return 'partials/app-mainsec.html?' + +new Date();
}

但可能只有在编译指令时它才会刷新你的指令。

关于javascript - AngularJS 指令 templateUrl 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24577945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com