将 css class 添加到 Angularjs 中标记的元素
Add css class to marked elements in Angularjs
我有一个 ToDo 列表,我希望在单击 "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>
.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>
这是myNoteCtrl.js
var app = angular.module('myApp', []);
app.controller('todoCtrl', function($scope) {
$scope.todoList = [{todoText:'Clean House', done:false},{todoText:'Clean House2', done:false}];
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
if (!x.done) x.todoText.class="strike";
});
};
});
您不应在任务的字符串 todoText
上添加 class
属性。您应该改为向任务添加 striked
布尔值 属性:
$scope.strike = function() {
angular.forEach($scope.todoList, function(x) {
if (!x.done) x.striked = true;
});
};
然后使用这个布尔值添加 css class:
<span ng-class="{strike: x.striked}" ng-bind="x.todoText"></span>
你需要使用 ng-class 来实现同样的效果
ng-class="{isStriked : x.done}"
代码
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
x.isStriked = x.done;
});
};
我有一个 ToDo 列表,我希望在单击 "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>
.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>
这是myNoteCtrl.js
var app = angular.module('myApp', []);
app.controller('todoCtrl', function($scope) {
$scope.todoList = [{todoText:'Clean House', done:false},{todoText:'Clean House2', done:false}];
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
if (!x.done) x.todoText.class="strike";
});
};
});
您不应在任务的字符串 todoText
上添加 class
属性。您应该改为向任务添加 striked
布尔值 属性:
$scope.strike = function() {
angular.forEach($scope.todoList, function(x) {
if (!x.done) x.striked = true;
});
};
然后使用这个布尔值添加 css class:
<span ng-class="{strike: x.striked}" ng-bind="x.todoText"></span>
你需要使用 ng-class 来实现同样的效果
ng-class="{isStriked : x.done}"
代码
$scope.strike = function() {
var oldList = $scope.todoList;
angular.forEach(oldList, function(x) {
x.isStriked = x.done;
});
};