【发布时间】:2018-11-24 14:11:01
【问题描述】:
为什么axios回调变化在angularjs中显示,不使用$apply
我在angularjs 上尝试axios 库,当我看到axios 回调中$scope 的更改被angular 检测到时,我感到很惊讶。我想我必须打电话给$apply,例如,当你使用setTimeout时。
// axios example
axios.get(url).then((response) => {
// Here I don't need $apply, why??
$scope.axiosResult = response.data;
});
// setTimeout example
setTimeout(() => {
// Here I need $apply for the timeoutResult to appear on the HTML
$scope.$apply(() => $scope.timeoutResult = {message: "timeout!"});
}, 2000)
你知道为什么axios不需要$apply吗?
编辑:
A comment by georgeawg 帮助我看到我在另一个地方使用了$http,所以我猜$http 触发的摘要循环也在帮助 axios 回调被“消化”。
【问题讨论】:
-
我从不使用 $apply,我使用了一堆 3rd 方 AngularJS 模块。您是否尝试过 $timeout (AngularJS) 而不是 setTimeout?
-
我的问题具体是为什么 axios 回调参与数据绑定,尽管我没有使用
$q或$apply。我想我错过了一些关于 angularjs 是如何工作的......
标签: angularjs angular-promise axios