Javascript 向Angularjs中标记的元素添加css类
我有一个待办事项列表,我希望当我单击“Strike marked”按钮时,所有选中的元素都被删除 这是我的代码index.htmlJavascript 向Angularjs中标记的元素添加css类,javascript,css,angularjs,angularjs-ng-class,Javascript,Css,Angularjs,Angularjs Ng Class,我有一个待办事项列表,我希望当我单击“Strike marked”按钮时,所有选中的元素都被删除 这是我的代码index.html <!DOCTYPE html> <html> <head> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <style> .st
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<style>
.strike {
text-decoration: line-through;
}
</style>
</head>
<body ng-app="myApp" ng-controller="todoCtrl">
<h2>My Todo List</h2>
<div ng-repeat="x in todoList">
<input type="checkbox" ng-model="x.done"><span ng-class="" ng-bind="x.todoText"></span>
</div>
<p>
<button ng-click="strike()">Strike marked</button>
</p>
<script src="myNoteCtrl.js"></script>
</body>
</html>
您不应该在任务的字符串
toText
上添加class
属性。您应该改为在任务中添加一个删除的布尔属性:
$scope.strike = function() {
angular.forEach($scope.todoList, function(x) {
if (!x.done) x.striked = true;
});
};
然后使用此布尔值添加css类:
<span ng-class="{strike: x.striked}" ng-bind="x.todoText"></span>
您需要使用ng类来实现相同的功能
ng-class="{isStriked : x.done}"
代码
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
x.isStriked = x.done;
});
};
@tanja很高兴能帮助你..看看plukr,它会让你有更清晰的想法。。
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
x.isStriked = x.done;
});
};