使用ng-class时,在更改表单之前,setTimeout不会执行

时间:2015-11-24 18:29:07

标签: javascript angularjs

我在js中使用超时功能:

setTimeout(function () {
            $scope.shadow = 'speller-blue';
            currIndex = Math.floor(Math.random() * 2);
            $scope.currCard = cards[currIndex];
        }, 1000);

我在我的html中使用ng-class:

<input id="speller-box" type="text" class="form-control ng-class:shadow" autofocus="" placeholder="answer" ng-model="answer">

我希望超时中的代码在一秒钟延迟后执行并将字段变为蓝色,但直到输入框中的值发生更改才会发生更改。

2 个答案:

答案 0 :(得分:1)

Angular不会知道setTimeout改变了什么,直到它完成下一个消化周期。在对范围进行更改后,可以使用$timeout或手动调用$digest

答案 1 :(得分:0)

我认为你做错的一件事是你正在应用条件类,但你使用的格式不正确

执行类似

的操作
<input id="speller-box" type="text" ng-class="{'form-control': shadow}" autofocus="" placeholder="answer" ng-model="answer">