Tuesday, 23 February 2016

How to add version with html template URL in AngularJS?

Following is the code which you can use to add version with html template URL, please look into it.

Web.config file:   

 <appSettings>
        <add key="CURRENT_VERSION" value="4_0"/>
  </appSettings>

Index.cshtml file:


 var CurrentVersion = '@ViewBag.CurrentVersion';

app.js file:

 angular.module('app', [])
.config(['$httpProvider', function ($httpProvider) {

        $httpProvider.interceptors.push('httpInterceptorVersion');
     
    }])

    .factory('httpInterceptorVersion', function () {

        return {
            request: function (config) {

                if ((config.method === "GET") && (config.url.match(/\.\html?$/))) {
                    config.url += '?v=' + CurrentVersion;
                    // here you can target particular folder file

                    //if (config.url.indexOf("template/") !== 0) {
                    //    config.url += '?v=' + currentVersion;
                    //}
                }
                return config;
            }
        };
    });

I hope it might be helpful to manage caching issue.