AngularJS V1.1拦截器最后总是有$ q.when [英] AngularJS V1.1 interceptor always have $q.when at the end
问题描述
在文档(1.1版)中AngularJS关于拦截器,拦截器函数都返回类似这样的东西
In the documentation (version 1.1) of AngularJS about interceptors, the interceptor functions all return something like this
return response || $q.when(response);
然而,在我的应用中,响应总是被定义,所以$ q.when(响应)从未被执行过。
所以问题是在什么情况下'响应'是不确定的,什么是
However, in my app, 'response' is always defined, so $q.when(response) is never executed. So the question is in what situation will the 'response' be undefined and what will
$q.when(response) // == $q.when(null)
do!因为响应是未定义/ null?
do! because response is undefined/null ?
推荐答案
-
$ q.when(承诺)
→承诺
-
$ q.when(nonPromise)
→一个新的承诺
,它将异步解析为给定值nonPromise
。 $q.when(promise)
→promise
$q.when(nonPromise)
→ a newpromise
, that will asynchronously resolve to the given valuenonPromise
.
让我们看看什么是 $ q.when
:
$q.when = function (foreignPromise) {
var deferred = $q.defer();
foreignPromise.then(function (data) {
deferred.resolve(data);
$rootScope.$digest();
}, function (reason) {
deferred.reject(reason);
$rootScope.$digest();
});
return deferred.promise;
}
工厂退货$ q.when(数据)
我们可以看到 $ q.when
收到promise或nonPromise并将其换行。
Factory return $q.when(data)
As we can see $q.when
receives promise or nonPromise and wrap it with.
工厂示例:
fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {
var data = [
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
];
var factory = {
query: function (selectedSubject) {
return $q.when(data);
}
}
return factory;
}]);
现在我们可以从控制器调用它:
Now we can call it from controller:
Data.query()
.then(function (result) {
$scope.data = result;
}, function (result) {
alert("Error: No data returned");
});
演示 小提琴
从这个例子中我们返回承诺。所以让我们稍微改变一下:
From this example we return promise. So lets change it a bit:
而不是返回$ q.when(数据);
我们将写:
return $q.when(data) || data;
它也可以。但反之亦然。
It will work as well. But not vice versa.
据我所知Angular知道控制器等待数据
服务承诺和上述声明将使用1st off $ q.when(数据)
。
As I understand Angular knows that controller waits from Data
service the promise and above mentioned statement will use 1st off $q.when(data)
.
演示2 小提琴
现在我们通过这种方式调用我们的 Data
服务:
Now lets call our Data
service by this way:
$scope.data = Data.query();
没有承诺,呼叫是同步的。
No promises, the call is sync.
出厂似乎:
fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {
var data = [
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
];
var factory = {
query: function (selectedSubject) {
return data || $q.when(data);
}
}
return factory;
}]);
演示3 小提琴
我的结论
返回数据|| $ q.when(data)
表示我们的服务可以返回单值或承诺。但是既然我们知道我们的服务返回了什么类型的数据,那么在这个声明中没有任何意义。或数据
或承诺
。
The return data || $q.when(data)
means that our service can return single value or promise. But since we know what type of data our service returns , there is no sense in this statement. Or data
or promise
.
这篇关于AngularJS V1.1拦截器最后总是有$ q.when的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!