AngularJS:重置angular.元素CSS改变

AngularJS: resetting angular.element css changes

本文关键字:CSS 改变 元素 angular 重置 AngularJS      更新时间:2023-09-26

我有一系列的按钮,我正在使用ng-repeat显示。按钮代表一组旋转问题的答案。如果他们单击正确答案按钮,我将更改按钮的样式并禁用它。所有这些都运行良好。然而,当我试图允许用户点击"重置"按钮时,它运行ng-init函数来重置所有变量值等,样式不是更新或刷新,而是保持相同的状态。参见代码片段:

HTML:

<div class="container" ng-app="MyApp" ng-controller="MyCtrl" ng-init="init()">
  <div>
    <div class="timer">
      Timer: <span ng-bind="counter"></span>
    </div>
    <br>
    <span>
      <button class="btn btn-lg btn-primary" ng-click="startCountdown()" ng-disabled="quizStart">Start</button>
      <button class="btn btn-lg btn-primary" ng-click="init()">Reset</button>
    </span>
  </div>
  <br>
  <fieldset ng-disabled="counterStop">
    <div ng-show='quizStart' ng-disabled="counterStop">
      <h3> {[{ question }]}: </h3>
      <br><br>
      <span class="child" ng-repeat="choice in choices">
        <button class="btn btn-lg btn-info my-btn" ng-click="clickThis($event, choice)"> {[{ choice }]} </button>
      </span>
    </div>
  </fieldset>
</div>

控制器:

var app = angular.module('MyApp', []);
var MyCtrl = function($scope, $timeout) {
  $scope.init = function() {    
    $scope.counter = 10;
    $scope.counterStop = true;
    $scope.quizStart = false;
    $scope.questionNum = 0;
    $scope.questions = [
      ['Here is question 1',
        [],
        {
          answer1: false,
          answer6: false
        }
      ],
      ['Here is question 2',
        [],
        {
          answer4: false,
          answer2: false
        }
      ],
      ['Here is question 3',
        [],
        {
          answer3: false,
          answer5: false
        }
      ]
    ];
    // set initial question and choices
    $scope.question = $scope.questions[$scope.questionNum][0];
    $scope.choices =     ['answer1','answer2','answer3','answer4','answer5','answer6'];
  }
  countdown = function() {
    $scope.counter--;
    if (!$scope.counterStop && $scope.counter > 0) {
       $timeout(countdown,1000);
     } else if ($scope.counter === 0) {
       $scope.counterDone = true;
       $scope.counterStop = true;
     }
   };
  $scope.startCountdown = function() {
    $scope.counterStop = false;
    $scope.quizStart = true;
    $timeout(countdown, 1000);
  };
   $scope.checkAnswerStatus = function() {
    for (var value in $scope.questions[$scope.questionNum][2]) {
      if ($scope.questions[$scope.questionNum][2][value] === false) return false;
    }
    return true;
  }
  $scope.clickThis = function(event, answer) {
    var correctAnswer = false;
    for (var value in $scope.questions[$scope.questionNum][2]) {
      if (answer === value) {
        $scope.rightAnswers += 1;
        element = angular.element(event.target);
        element.css({'color': 'white', 'background-color': 'green'});
        element.prop('disabled', true);
        $scope.questions[$scope.questionNum][2][value] = true;
        correctAnswer = true;
      }
    }
    if (correctAnswer === false) {
      $scope.wrongAnswers += 1;
      element = angular.element(event.target);
      element.css({'color': 'white', 'background-color': 'red'});
    }
    if ($scope.checkAnswerStatus()) {
      // make sure to check if there are any more questions, and if not, elegantly end
      if ($scope.questionNum === $scope.questions.length-1) {
        $scope.counterStop = true;
      }
      else {
        $scope.questionNum += 1;
        $scope.question = $scope.questions[$scope.questionNum][0];
      }
    }
  }  
};
app.controller('MyCtrl', ['$scope', '$timeout', MyCtrl]);

在ClickThis函数中,我禁用按钮并根据正确与否更改样式。但是,当我再次调用init()时,在用户单击reset之后,样式更改仍然存在。

是否有办法让它刷新或至少获得访问这些元素再次(即my-btn类)重新设置样式?

谢谢!

  element = angular.element(event.target);
  element.css({'color': 'white', 'background-color': 'red'});

这样做,你是在使用jqLite来设置样式,这是错误的,首先,因为你不应该触摸DOM(特别是在控制器中),其次,因为这不是Angular的方式。

这是一个使用ngClassngDisabled的更有棱角的解决方案:

<<p> 视图/strong>
<button class="btn btn-lg btn-info my-btn"
        ng-class="choice.buttonClass"
        ng-click="clickThis($event, choice)"
        ng-disabled="choice.buttonDisabled"> {[{ choice }]} </button>
控制器

$scope.clickThis = function(event, answer) {
  // Check if the answer is correct or wrong
  // ...
  // ...
  // Set the class of the button according to the correctness
  answer.buttonClass = correctAnswer ? 'answer-correct' : 'answer-wrong';
  // Disable the button according to the correctness
  answer.buttonDisabled = correctAnswer;
}

如果你想保持你的方式和使用jqLite的方式,你可以简单地使用element.removeAttr('style')来删除所有以前应用的样式。

你应该使用指令ng-disabled来禁用按钮,或者使用指令ng-class来改变元素的类和样式,而不是修改控制器中的样式。

https://docs.angularjs.org/api/ng/directive/ngDisabledhttps://docs.angularjs.org/api/ng/directive/ngClass