最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS directive templateUrl doesn't update - Stack Overflow

programmeradmin1浏览0评论

I have a problem with angular directives. When I edit the content of a file referenced via templateUrl, the result doesn't appear until I delete the cache. I have the following code:

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;
}]);

app-mainsec.html

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

and index.html

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

When I change <h1>Hi</h1> to <h1>Hello</h1>, the view of directive doesn't update until I delete the cache.

I have a problem with angular directives. When I edit the content of a file referenced via templateUrl, the result doesn't appear until I delete the cache. I have the following code:

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;
}]);

app-mainsec.html

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

and index.html

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

When I change <h1>Hi</h1> to <h1>Hello</h1>, the view of directive doesn't update until I delete the cache.

Share Improve this question edited Jul 9, 2014 at 17:38 drs 5,7974 gold badges47 silver badges68 bronze badges asked Jul 4, 2014 at 16:04 user3461226user3461226 4
  • I cant make out what you're trying to achieve here, where is your controller and your bindings for the <h1> ? – Pogrindis Commented Jul 4, 2014 at 16:17
  • It isn't a problem, that's how Angular works. If you want Hi to be Hello create a scope variable and put it on your template. – lucuma Commented Jul 4, 2014 at 16:23
  • You don't need to change title with "(Solve)", if you accept the answer people will see that's solve. – jcubic Commented Jul 7, 2014 at 13:00
  • @jcubic ok, I'm new for here. thanks again – user3461226 Commented Jul 7, 2014 at 13:06
Add a ment  | 

1 Answer 1

Reset to default 8

The reason for that is that Angular fetch a file only once at the begining. You can try to use templateUrl as function and append timestamp so you get new template url each time.

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

But probably, it will refresh your directive only when directive will be piled.

发布评论

评论列表(0)

  1. 暂无评论