每次ng-include指令请求部分时,我想更改URL.到目前为止,我可以看到url和这样的事件:
app.run(function ($rootScope) { $rootScope.$on('$includeContentRequested',function (event,url) { console.log(event); console.log(url); }); });
现在我需要将“templates / incs / includedPartial.html”的url更改为“templates / incs / includedPartial.html?cache_version = 1_1”,然后将该部分包含在新的链接中.
显然,我正在这样做,以防止版本更改的缓存问题.这是一个好的策略还是有更好的解决方案?提前感谢任何帮助…
我想我真的想出了这个答案.你可以做的是创建拦截器.由于使用ng-include的所有请求实际上都会通过通用的$httpProvider来拦截请求并添加缓存无效.
app.factory( "cacheBusterFactory",[ "VERSION",function( VERSION ) { return { request: function( config ) { if( config.url.indexOf( ".html",config.url.length - ".html".length ) !== -1 ) { config.url += "?v=" + VERSION.toString(); } return config; } }; } ] );
在这种情况下,“VERSION”是每次部署时更改的角常数:
app.constant( "VERSION",0.1 );
添加缓存无效化就像:
.config( [ "$httpProvider",function( $httpProvider ) { $httpProvider.interceptors.push( "cacheBusterFactory" ); } ] )
正如你可以看到,我只拦截.html文件,因为那些是我需要添加缓存破坏的唯一的.您当然可以扩展或重建“cacheBusterFactory”来满足您的需要.