In angular.js, $scope.greeting = xxx
doesn't work in window.setTimeout
. It has not any effect:
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);
})
Why?
The full comparison is below:
window.setTimeout
): http://jsfiddle.net/victorwoo/3b3s6ukp/2/
setTimeout
operates outside of the $digest
cycle - therefore angular doesn't know about the change that you applied to $scope
.
Instead of window.setTimeout
, use the built in $timeout
function
See this (your) updated jsfiddle here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With