AngularJS指令templateUrl不会更新

5 javascript angularjs angularjs-directive

我有角度指令的问题.当我编辑引用的文件的内容时,templateUrl直到我删除缓存后才会显示结果.我有以下代码:

Directive.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;
}]);
Run Code Online (Sandbox Code Playgroud)

APP-mainsec.html

<div><h1>Principal</h1></div>
Run Code Online (Sandbox Code Playgroud)

和index.html

...
 <div app-mainsec></div>
...
Run Code Online (Sandbox Code Playgroud)

当我改为<h1>Hi</h1><h1>Hello</h1>,指令的视图在我删除缓存之前不会更新.

jcu*_*bic 7

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

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

但可能只有在编译指令时才会刷新指令.