由于一些基础设施的变化(即服务器和VPN),有时我想在离线模式下运行我们的应用程序.我已经能够使用ngMockE2E实现这一点,但它似乎是一种全有或全无的方法,这意味着您必须从应用程序中明确设置每个HTTP请求.
有没有办法让它假设除非你已经明确设置了一个路由/ url来处理它将自动调用一个通用的passThrough()操作?
目前我这样做:
noSrvc = $location.search().hasOwnProperty 'nosrvc' # # templates # $httpBackend.whenGET /(\.htm|\.html)$/ .passThrough(); # # session # rqst = $httpBackend.whenGET /(api\/users\/current)$/ if !noSrvc then rqst.passThrough() else rqst.respond {"user":{ # doing something similar for every single service call in the app... gets tedious after about 3-4
这是我用于白名单的配方
app.run(function ($httpBackend) { // mocked requests,should come first $httpBackend.when('GET','mock').respond(200,{}); // whitelisted real requests,should come last angular.forEach(['GET','DELETE','JSONP','HEAD','PUT','POST','PATCH'],function (method) { $httpBackend.when(method).passThrough(); }); });
而且我很确定优先权在这里很重要.