我有一个指令对从输入元素触发的 Blur 事件使用react,然后最终导致调用 Angular $http 服务以发出适当的 HTTP 请求。
$http 服务被正确调用并生成预期的 promise ,但是实际的 HTTP 请求不会立即发出。只有在进行其他类型的 DOM 交互后才会触发。
为什么不立即提出请求?我怎样才能强制它们立即制作?
Angular v1.1.5
最佳答案
原因
我最终在 GitHub ( https://github.com/angular/angular.js/issues/2442 ) 上的问题中找到了答案:
v1.1.4... changed the way $http worked so that it was scheduled on nextTick instead of firing immediately. This means that if your $http is scheduled from something that doesn't cause a $digest to fire then your request won't be sent. You can verify that this is the behavior you're seeing by clicking on the page a few times (anywhere) and you should see your request go out.
一个办法
正如帖子所建议的那样,我触发了一个 $digest 循环,这导致我的 $http 生成的请求立即被触发。
scope.$apply();
关于angularjs - Angular 没有立即发出 $http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039998/