Angular:update $ scope在setTimeout回调中不起作用

vic*_*woo 5 javascript angularjs

在angular.js中,$scope.greeting = xxx不起作用window.setTimeout.它没有任何影响:

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function ($scope) {
    $scope.greeting = 'init';
    window.setTimeout(function () {
        console.log('update greeting');
        $scope.greeting = "hello"; // doesn't work here.
    }, 3000);
})
Run Code Online (Sandbox Code Playgroud)

为什么?

完整比较如下:

Cas*_*mer 16

setTimeout$digest循环之外运行 - 因此角度不知道您应用的变化$scope.

而不是window.setTimeout使用内置$timeout函数

在这里看到这个(你的)更新的jsfiddle