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.

   
 

4 comments:

  1. Thanks Blog, Its working for me......

    ReplyDelete
  2. Nice blog, it helped me lot.

    ReplyDelete
  3. Nice stuff! It's working fine for me.

    ReplyDelete
  4. Thank you.Well it was nice post and very helpful information on
    AngularJS4 Online Course

    ReplyDelete