Shlomo
Shlomo

Reputation: 3990

model not changed with ng-show in setTimeout

Trying to toggle a div based on model value (true, false) and ng-show. Together with a setTimeout it does not work anymore. What is the reason?

<div ng-app>
<div ng-controller="Ctrl">
    <div ng-show="abc">Div 1</div>
    <div ng-show="abc">Div 2</div>     
</div>
</div>

function Ctrl($scope) {
    setTimeout( function() {
        alert( "yeah" );
        $scope.abc = true;
    }, 1000 );
}

See this fiddle.

Upvotes: 0

Views: 159

Answers (2)

ms87
ms87

Reputation: 17492

Use $timeout instead, setTimeout doesn't force a digest since it isn't angular, so angular has no idea something has changed, $timeout calls a $scope.$apply() automatically, thus notifying watchers of changes.

Upvotes: 1

tymeJV
tymeJV

Reputation: 104795

setTimeout doesnt trigger a digest cycle. You should be using Angular's $timeout service.

function Ctrl($scope, $timeout) {
    $timeout( function() {
        alert( "yeah" );
        $scope.abc = true;
    }, 1000 );
}

Upvotes: 1

Related Questions