Avoid angularJS partial views to be cached in IE

2019-04-02 02:33发布

I'm working on an ASP.NET MVC application that also has some angularJS. I have a main page that has different tabs that when you click them they load an angular partial view. This is how the main page looks like:

{ ... }     
<div class="widget-div" ng-controller="mainController"> 
    <div class="widget-body">
        <div class="tabs-left">
            <ul id="demo-pill-nav" class="nav nav-tabs tabs-left" style="width: 160px">
                <li ng-class="getMenuClass('/search')">
                    <a href="javascript:void(0);" ng-click="selectPage('search')">Search</a>
                </li>
                <li ng-class="getMenuClass('/addressVerification')">
                    <a href="javascript:void(0);" ng-click="selectPage('addressVerification')">Address Verification</a>
                </li>
                <li ng-class="getMenuClass('/dashboard')">
                    <a href="javascript:void(0);" ng-click="selectPage('dashboard')">Dashboard</a>
                </li>
                <li ng-class="getMenuClass('/editProfile')">
                    <a href="javascript:void(0);" ng-click="selectPage('editProfile')">Update Profile</a>
                </li>               
            </ul>
            <div class="tab-content">
                <div class="active tab-pane">
                    <ng:view />
                </div>
            </div>
        </div>
    </div>  
</div>
{ ... } 

In the main controller, the selectPage method looks like:

$scope.selectPage = function (page) {
    //not relevant code removed here
    $location.path("/" + page);
};

And of course I have defined the routes like:

$routeProvider.when("/search", {
    templateUrl: "/Scripts/app/views/search.html"
});

Now, what I need is these partial views to not get cached, at least while in development. For that I used the method provided here:

myApp.run(function($rootScope, $templateCache) {
   $rootScope.$on('$viewContentLoaded', function() {
      $templateCache.removeAll();
   });
});

That worked perfect except in Internet Explorer, which always will load the cached version even when hitting Ctrl+F5.

Also, I tried setting the HTTP headers in the layout html, like this:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

But that didn't work.

So my question is if there is any way to avoid this behavior.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-02 02:59

use below tag in web.config to disable caching

   <staticContent>
       <clientCache cacheControlMode="DisableCache" />
    </staticContent>
查看更多
smile是对你的礼貌
3楼-- · 2019-04-02 03:16

I found a workaround that is exactly what I need. Adding a version number to the route like this:

$routeProvider.when("/search", {
    templateUrl: "/Scripts/app/views/search.html" + version
});

where version is a string like: ?v1.0.0

That way any time I make a change I just change the version number and when hitting that page the partial view will be downloaded again.

查看更多
登录 后发表回答