微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/angularjs/140768.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐