Dejan.S Dejan.S - 6 months ago 21
AngularJS Question

change scope property with setTimeout

Why doesn't it work to set the

$scope.blured
within this
setTimeout()
(simulating a post and response)?

Plunker

$scope.bluryLines = function(value) {
$scope.blured = true;

if (value === '' || value === undefined) {
console.log('value is empty');
} else {
console.log(value);
}

//faking a post
setTimeout(function() {
$scope.blured = false;
console.log('log');
}, 1000);
};


pushing the button cleares it right away.

$scope.removeOverlay = function() {
$scope.blured = false;
};

Answer

setTimeout doesn't run a digest. Use the angular $timeout service instead.

app.controller('MainCtrl', function($scope, $timeout) {

    $scope.bluryLines = function(value) {
        $scope.blured = true;

        //faking a post
        $timeout(function() {
            $scope.blured = false;
        }, 1000);
    };

});
Comments