我在动态更新 CSS 时遇到问题。这是我的 HTML 代码:
<!DOCTYPE html>
<html lang="en" ng-app="ListItems">
<head>
<meta charset="utf-8">
<title>Test project</title>
<link rel="stylesheet" href="Content/bootstrap.css">
<link rel="stylesheet" href="Content/style.css">
</head>
<body ng-controller="ListController as list">
<h1>To-do list</h1>
<div id="main">
<article ng-repeat="item in items track by $index" ng-class="{{item.IsCompleted ? 'completed' : 'uncompleted'}}">
<p>{{item.Description}}</p>
<footer>
<small>
Is completed: <input type="checkbox" ng-checked="{{item.IsCompleted}}" ng-click="complete($index)" />
</small>
</footer>
</article>
</div>
<script src="Scripts/angular.js"></script>
<script src="Scripts/jquery-3.1.1.js"></script>
<script src="Scripts/script.js"></script>
</body>
</html>
现在我想在元素完成时应用以下 css 样式:
.completed {
background-color: red;
}
但是什么也没发生。
最佳答案
尝试使用正确的 ng-class
语法并确保 .completed
CSS 类由于您的 CSS 声明而可用。这是一个简单的工作 fiddle这将帮助你。将其与您的解决方案进行比较。请看ng-class documentation的 AngularJS。
<article ng-repeat="item in items track by $index"
ng-class="{'completed': item.IsCompleted, 'uncompleted': !item.IsCompleted}">
<p>{{item.Description}}</p>
<footer>
<small>
Is completed:
<input type="checkbox"
ng-checked="{{item.IsCompleted}}"
ng-click="complete($index)" />
</small>
</footer>
</article>
关于html - 使用 ngClass 时 CSS 样式不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207333/