使用ngClass

时间:2017-02-13 15:13:06

标签: html css angularjs ng-class

我有动态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;
}

但什么都没发生。

2 个答案:

答案 0 :(得分:1)

尝试使用正确的ng-class语法,并确保由于您的CSS声明,.completed CSS类可用。这是一个简单的工作fiddle,它将为您提供帮助。与您的解决方案进行比较。请查看AngularJS的ng-class documentation

<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>

答案 1 :(得分:0)

您需要更改ng-class语法。简单说明:

<article ng-repeat="item in items track by $index" ng-class="{'completed': item.IsCompleted}">