AngularJS V1.1 拦截器最后总是有 $q.when [英] AngularJS V1.1 interceptor always have $q.when at the end

查看:27
本文介绍了AngularJS V1.1 拦截器最后总是有 $q.when的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档(1.1 版)中AngularJS关于拦截器,拦截器函数都返回这样的

返回响应 ||$q.when(响应);

但是,在我的应用程序中,始终定义了响应",因此永远不会执行 $q.when(response).所以问题是在什么情况下响应"是未定义的,什么会

$q.when(response)//== $q.when(null)

做!因为响应未定义/空?

解决方案

  • $q.when(promise)promise
  • $q.when(nonPromise) → 一个新的 promise,它将异步解析为给定的值 nonPromise.

让我们看看什么是$q.when:

$q.when = function (foreignPromise) {var deferred = $q.defer();foreignPromise.then(函数(数据){deferred.resolve(data);$rootScope.$digest();}, 函数(原因){deferred.reject(reason);$rootScope.$digest();});返回 deferred.promise;}

工厂退货 $q.when(data)

正如我们所见,$q.when 接收到 promise 或 nonPromise 并用它包装起来.

工厂示例:

fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {变量数据 = [{"PreAlertInventory": "5.000000","SharesInInventory": "3.000000","TotalSharesSold": "2.000000","TotalMoneySharesSold": "18.000000","TotalSharesBought": "0.000000","TotalShareCost": "0.000000",估计损失":0.000000"}];var 工厂 = {查询:函数(selectedSubject){返回 $q.when(data);}}返厂;}]);

现在我们可以从控制器调用它:

Data.query().then(函数(结果){$scope.data = 结果;}, 函数(结果){alert("错误:没有返回数据");});

演示 小提琴

工厂返回 $q.when(data) ||数据

在这个例子中,我们返回了 promise.所以让我们稍微改变一下:

取而代之的是return $q.when(data);我们会这样写:

return $q.when(data) ||数据;

它也会起作用.但反之则不然.

据我所知,Angular 知道控制器从 Data 服务等待,promise 和上面提到的语句将使用 $q.when(data) 的 1st off.

演示 2 小提琴

工厂返回数据 ||$q.when(数据)

现在让我们通过这种方式调用我们的Data服务:

$scope.data = Data.query();

没有承诺,调用是同步的.

出厂看起来像:

fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {变量数据 = [{"PreAlertInventory": "5.000000","SharesInInventory": "3.000000","TotalSharesSold": "2.000000","TotalMoneySharesSold": "18.000000","TotalSharesBought": "0.000000","TotalShareCost": "0.000000",估计损失":0.000000"}];var 工厂 = {查询:函数(selectedSubject){返回数据 ||$q.when(数据);}}返厂;}]);

演示 3 小提琴

我的结论

返回数据 ||$q.when(data) 意味着我们的服务可以返回单个值或承诺.但是既然我们知道我们的服务返回的是什么类型的数据,这个说法就没有意义了.或 datapromise.

In the documentation (version 1.1) of AngularJS about interceptors, the interceptor functions all return something like this

return response || $q.when(response);

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! because response is undefined/null ?

解决方案

  • $q.when(promise)promise
  • $q.when(nonPromise) → a new promise, that will asynchronously resolve to the given value nonPromise.

Lets see what is $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;
}

Factory return $q.when(data)

As we can see $q.when receives promise or nonPromise and wrap it with.

Factory example:

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");
           });

Demo Fiddle

Factory returns $q.when(data) || data

From this example we return promise. So lets change it a bit:

Instead return $q.when(data); we will write:

return $q.when(data) || data;

It will work as well. But not vice versa.

As I understand Angular knows that controller waits from Data service the promise and above mentioned statement will use 1st off $q.when(data).

Demo 2 Fiddle

Factory returns data || $q.when(data)

Now lets call our Data service by this way:

$scope.data =  Data.query();

No promises, the call is sync.

Out factory seems like:

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;
}]);

Demo 3 Fiddle

My Conclusion

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屋!

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