Angularjs Angular.js为什么本机异步函数不更新$scope变量?

Angularjs Angular.js为什么本机异步函数不更新$scope变量?,angularjs,asynchronous,Angularjs,Asynchronous,我试图理解如何处理角度视图更新。当我使用本机异步函数并且回调更新$scope变量时,视图不会更新。但当我使用角度异步函数时,视图会正确更新。例如: // Example 1: This code updates the store in view $timeout(function() { $scope.store = { name: 'MyName' } }, 2000); // Example 2: This code does not update the

我试图理解如何处理角度视图更新。当我使用本机异步函数并且回调更新$scope变量时,视图不会更新。但当我使用角度异步函数时,视图会正确更新。例如:

// Example 1: This code updates the store in view
$timeout(function() {
    $scope.store = {
        name: 'MyName'
    }
}, 2000);

// Example 2: This code does not update the store in view
setTimeout(function () {
    $scope.store = {
        name: 'MyName'
    }
}, 2000);

为什么第二个示例不更新存储?

这是因为Angular服务
$timeout
知道Angular运行时并将正确注册更改

在进行更改后,通过调用作用域上的$apply(),vanilla js可以捕获相同的行为:

setTimeout(function () {
  $scope.store = {
    name: 'MyName'
  }
  $scope.$apply();
}, 2000);

还有像
$window
$interval
这样的服务,它们作为常规JS的方便包装,但它们确保所有$scope更改都正确注册到摘要周期中。

,因为第二个示例不知道摘要周期是什么,也不更新它(这就是某些本机函数被转换的原因)。可能重复如何包装本机异步函数以实现视图更新?我不想使用$scope.apply()。在不进入Angular的核心的情况下,您必须使用$apply来注册您的$scope更改。为什么您不想使用它?因为我正在使用自定义ORM,并且它的api是异步的。