简体   繁体   English

使用 angularjs 更改背景颜色

[英]change background color with angularjs

Continue discovering angularjs and stucked at changing colors on object, if it true - for ex.继续发现 angularjs 并坚持改变对象的颜色,如果它是真的 - 例如。 green, if false another color.绿色,如果是假的另一种颜色。 I've tried to realized it but it shows me to all rendered data one color:我试图实现它,但它向我展示了所有渲染数据的一种颜色:

rssFeedService.getFeed().then(function (results) {
    $scope.feed = results.data;

    $scope.feed.forEach(function (checkitem){
        if (!checkitem.isRead) {
            $scope.read = {
                "color": "white",
                "background-color": "coral"
            }
        } else {
            $scope.read = {
                "color": "white",
                "background-color": "green"
            }
        }
    });

}, function (error) {
    //alert(error.data.message);
});

Here i'm getting data and with loop i'm set the color.在这里,我正在获取数据并使用循环设置颜色。

<div class="col-md-12" ng-repeat="q in feed">

    <a href="{{q.link}}">
        <h1 ng-click="isReadClick(q.id)" ng-style="read"> {{q.title}}</h1>
    </a>

    <h4>{{q.body}}</h4>

</div>

I Suppose that this happens because I'm using ng-style and should use ng-class for set static color.我假设发生这种情况是因为我使用的是 ng-style 并且应该使用 ng-class 来设置静态颜色。

If you are using ng-repeat then I would recommend ng-class.如果您使用的是 ng-repeat,那么我会推荐 ng-class。

<h1 ng-click="isReadClick(q.id)" ng-class="q.isRead ? 'green' : 'red'"> {{q.title}}</h1>

In the example, if q.isRead value is a true then set class green else set class red .在示例中,如果q.isRead值为 true,则设置 class green否则设置 class red

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM