为嵌入式NG-模板执行角HTTP拦截 [英] Angular HTTP interceptor executed for embedded ng-templates

查看:95
本文介绍了为嵌入式NG-模板执行角HTTP拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角拦截工作:

factory('myHttpInterceptor', function ($q, $location, $rootScope) {
// do something
return function (promise) {
    return promise.then(function (response) {
        // do something
        return response;
    }, function (response) {
        // do something
        return $q.reject(response);
    });
};
})

和一个大的HTML文件,其中包含如&LT模板;脚本类型=文/ NG-模板ID =回家模板> 。不幸的是我的HTTP拦截器拦截不仅装载HTTP请求,但还装载模板(已经加载HTML文件),其定义如时,('/',{控制器控制器:MainController,templateUrl:回家-template'})。有没有办法如何使拦截器拦截只HTTP请求或如何认识我是否加载来自服务器的东西,或只是一个模板?

and one big html file which contains templates like <script type="text/ng-template" id="home-template">. Unfortunately my HTTP interceptor intercepts not only loading HTTP requests but also loading templates (which are already loaded in html file) for controllers which are defined like when('/', {controller:MainController, templateUrl:'home-template'}). Is there a way how to make interceptor intercepting only HTTP requests or how to recognize whether I am loading something from server or just a template?

推荐答案

我跑这个问题为好。我们将查询字符串与拦截所有的$ HTTP调用。它结束了打破了我们的模板,因为在$ templateCache看的时候没有被发现的查询字符串模板名称(模板最初只是用它的ID缓存)。

I ran in to this issue as well. We were adding query strings to all our $http calls with an interceptor. It ended up breaking our templates, because when looking in $templateCache the template name with query string wasn't being found (the template was originally cached with just using it's id).

角$ httpProvider拦截器将拦截的$ HTTP模块调用。这些$ HTTP调用不一定是真正的HTTP GET / POST请求,他们也可以调用得到模板$ templateCache。这似乎是,当嵌入模板被引用,首先使用$ HTTP模块(其运行拦截第一),然后将$ HTTP模块将查找$ templateCache看如果模板已经缓存。如果$ HTTP发现该项目存在于$ templateCache它将返回它,如果不是会尝试建立实际的HTTP请求来获取模板。

Angular $httpProvider interceptors will intercept $http module calls. These $http calls are not necessarily real HTTP GET / POST requests they can also be calls to get templates in $templateCache. It seems like when an embedded template is being referenced, first the $http module is used (which run the interceptor first) and then the $http module will look in $templateCache to see if the template is already cached. If $http finds out the item exists in $templateCache it will return it, if not it will attempt to make an actual HTTP request to get the template.

我们的解决方案是包括在我们的拦截器$ templateCache模块和第一手动检查是否在$ templateCache存在的http请求。如果请求是不是在$ templateCache添加我们的查询字符串,如果是在$ templateCache然后返回它。

Our solution was to include the $templateCache module in our interceptor and manually check first if the http request exists in $templateCache. If the request is not in $templateCache add our query string, if it is in $templateCache then simply return it.

$httpProvider.interceptors.push(function($templateCache) {
    return {
        'request' : function(request) {
            // If the request is a get and the request url is not in $templateCache
            if(request.method === 'GET' && $templateCache.get(request.url) === undefined) {
                // Item is not in $templateCache so add our query string
                request.url = request.url + '?time=' + new Date().getTime();
            }
            return request;
        }
    };
});

这篇关于为嵌入式NG-模板执行角HTTP拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆