加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 更改$includeContentRequested上的源URL

发布时间:2020-12-17 09:15:19 所属栏目:安全 来源:网络整理
导读:每次ng-include指令请求部分时,我想更改URL.到目前为止,我可以看到url和这样的事件: app.run(function ($rootScope) { $rootScope.$on('$includeContentRequested',function (event,url) { console.log(event); console.log(url); });}); 现在我需要将“tem
每次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”来满足您的需要.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读