为什么我的ng-class即使在出现错误时也会显示

时间:2017-01-15 20:57:28

标签: javascript css angularjs ng-class angular-ng-class

当用户猜出鱼的名字错误时,我有一个想要动摇的图像。我正在使用条件ng-class="{'shake':error}"。但是,即使答案正确,图像也会抖动。我不相信在任何时候$scope.error都设置为真。我在这里错过了什么?

codepen

1 个答案:

答案 0 :(得分:0)

我认为您要做的是return guessIsCorrect或guessIsWrong来自compare函数。

$scope.compare = function(guess) {
    guess = guess.replace(/\s/g, '').toLowerCase();
    var answers = [];
    answers.push($scope.name.replace(/\s/g, '').toLowerCase());

    currentAnimal.alts.forEach(function(alt) {
        answers.push(alt.toLowerCase().replace(/\s/g, ''));
    });
    //console.log(answers);
    //console.log("Guess: " + guess + "\n");
    //console.log("Answer: " + answers + "\n");

    for (var x = 0; x <= answers.length; x++) {
        if (guess === answers[x]) {
            return guessIsCorrect();
        }
        if (x === answers.length) {
            return guessIsWrong();
        }
    }

};