angularjs – 更改$includeContentRequested上的源URL

每次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”来满足您的需要.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...