2014-06-06 53 views
3
app.config(function(RestangularProvider) {   
    RestangularProvider.addRequestInterceptor(function(element) { 
     console.log("Request started"); 
     return element; 
    }); 

    RestangularProvider.addResponseInterceptor(function(data) { 
     console.log("Request returned"); 

     return data; 
    }); 
}); 

我想弄清楚如何为请求发生时创建一个微调器。我怀疑这通常是通过在请求开始时显示一个,并在请求完成时隐藏它来完成的。Restangular Spinner

我怎样才能做到这一点与角和Restangular?我有拦截器设置如上,但这是在.conifg(),所以我不能访问$ rootScope或任何东西来跟踪任何div的可见性。

+0

退房这个插件:http://chieffancypants.github.io/angular-加载栏/我还没有研究过它是如何工作的,但他几乎接触到了所有HTTP请求,似乎处理得很好。我的Restangular设置完美无瑕。 – jraede

+0

我知道那个插件;我不想简单的解决......;) – Genu

回答

7

事实证明,我可以配置Restangular的run()块里面,让我获得了$rootscope像这样:

app.run(function($rootScope, Restangular) { 
    Restangular.addRequestInterceptor(function(element) { 
     $rootScope.xhr = true; 

     return element; 
    }); 
    Restangular.addResponseInterceptor(function(data) { 
     $rootScope.xhr = false; 

     return data; 
    }); 
}); 
+0

这在加载多个资源的同时有一个bug,你需要计算请求的数量 – Blowsie

+0

@Blowsie只是检查微调器是否已经设置为真正不要求数! – ProllyGeek

+0

@ProllyGeek对我没有意义,如果你有1或100个未决请求,它的设置为true – Blowsie