Dejan.S
Dejan.S

Reputation: 19118

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;
};

Upvotes: 1

Views: 145

Answers (1)

hansmaad
hansmaad

Reputation: 18905

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);
    };

});

Upvotes: 2

Related Questions