AngularJS:为什么$ HTTP,直到你离开当前摘要没有真正发挥http请求?到你、摘要、AngularJS、HTTP

2023-09-14 00:23:31 作者:青桔

在AngularJS文档$ HTTP在 http://docs.angularjs.org/api/ng $ HTTP指出$ HTTP服务实际上不会发送请求,直到下一个$摘要()被执行。

The AngularJS documentation for $http at http://docs.angularjs.org/api/ng.$http states "The $http service will not actually send the request until the next $digest() is executed.".

我只是想知道为什么它不只是把HTTP请求立即?这似乎是不必要的要求,我不知道我错过了什么。

I was just wondering why it does not just make the HTTP request immediately? It seems like an unnecessary requirement, I wonder what I am missing.

更新:它有人认为这是必要的,因为完成处理需要更新摘要。然而,这些完成事件需要做出自己的$应用/ $消化打过去了两种方式。

UPDATE: Its been suggested that this is necessary because the completion handlers will need to update the digest. However, those completion events will need to make their own $apply/$digest call later either way.

推荐答案

这是因为$ HTTP是基于承诺实际上是。在这里充分的解释: https://github.com/angular/angular.js /问题/ 2794#issuecomment-18807158

It's actually because $http is promise based. Full explanation here: https://github.com/angular/angular.js/issues/2794#issuecomment-18807158