Disable template caching in AngularJS with ui-router Disable template caching in AngularJS with ui-router angularjs angularjs

Disable template caching in AngularJS with ui-router


You can use a decorator and update UI Router's $templateFactory service to append a suffix to templateUrl

function configureTemplateFactory($provide) {    // Set a suffix outside the decorator function     var cacheBuster = Date.now().toString();    function templateFactoryDecorator($delegate) {        var fromUrl = angular.bind($delegate, $delegate.fromUrl);        $delegate.fromUrl = function (url, params) {            if (url !== null && angular.isDefined(url) && angular.isString(url)) {                url += (url.indexOf("?") === -1 ? "?" : "&");                url += "v=" + cacheBuster;            }            return fromUrl(url, params);        };        return $delegate;    }    $provide.decorator('$templateFactory', ['$delegate', templateFactoryDecorator]);}app.config(['$provide', configureTemplateFactory]);


Above solution was great and was not working for template urls

function templateUrl: function($stataParams) { return '/serverUrl?id=' + $stataParams.id + '&cid=' + $stataParams.cid; }

fixed by

function templateFactoryDecorator($delegate) {    var fromUrl = angular.bind($delegate, $delegate.fromUrl);    $delegate.fromUrl = function (url, params) {        if (url !== null && angular.isDefined(url)) {            if (typeof url == 'function') {                url = url.call(url, params);            }            if (angular.isString(url)) {             url += (url.indexOf("?") === -1 ? "?" : "&");             url += "v=" + Date.now().toString();             }         }         return fromUrl(url, params);     };     return $delegate;}