5

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.

drs
  • 5,679
  • 4
  • 42
  • 67
  • I cant make out what you're trying to achieve here, where is your controller and your bindings for the

    ?

    – Pogrindis Jul 04 '14 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 Jul 04 '14 at 16:23
  • You don't need to change title with "(Solve)", if you accept the answer people will see that's solve. – jcubic Jul 07 '14 at 13:00
  • @jcubic ok, I'm new for here. thanks again –  Jul 07 '14 at 13:06

1 Answers1

7

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 compiled.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • It's worth noting that this will disable browser caching of that html file. – Jon Snow Jul 07 '14 at 10:36
  • @JonSnow if cache is needed then there no way without using a server. You will need to watch a file for changes and send notification (you can use websockets) and then updated the date in templateURL. But I don't think something like this is needed, because something like this is only for development. When app is ready he can replace function with a string. – jcubic Jul 07 '14 at 12:55