AngularJS:重置angular.元素CSS改变
AngularJS: resetting angular.element css changes
我有一系列的按钮,我正在使用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的方式。
这是一个使用ngClass
和ngDisabled
的更有棱角的解决方案:
<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
相关文章:
- CSS或JS-当输入被聚焦时,改变输入的背景颜色
- 纯JavaScript中的Onclick滑块 - 事件不会改变CSS值
- HTML 按钮到大/CSS 不改变大小
- JavaScript 如果不是改变 CSS 的青春期
- CSS在IMG周围添加一个胖“框架”,而不改变IMG的位置
- JavaScript:任何改变CSS伪:before和:after的机会或替代方案
- JavaScript 可以改变@page CSS 的值吗?
- 为什么使用 grunt-contrib-cssmin 会改变我的 css,而它应该只缩小它
- AngularJS:重置angular.元素CSS改变
- 麻烦的CSS改变滑块上的开始位置
- 如何在不影响其他模态的情况下用CSS改变模态的宽度?
- 可以'不要用CSS改变表格中文本的颜色
- CSS改变样式/动画样式使用类或javascript
- Jquery/Javascript在CSS改变后只运行一次自定义函数
- jQuery/CSS -改变CSS类悬停的元素相同的href
- 拖,CSS改变后,Drop脚本开始崩溃
- 使用jQuery/javascript/css改变flash视频对象的属性
- 用CSS改变DOM元素顺序(鳄鱼)
- 如何动态地添加一个类到li项目,并使用javascript和css改变它的背景颜色
- 下拉选择器使用JS和CSS改变DIV的颜色