2

Continue discovering angularjs and stucked at changing colors on object, if it true - for ex. 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.

3
  • Try console.logging the read object in the javascript, and try to print it in the html (<pre ng-bind="read | json"></pre>). Do they match? Commented Mar 29, 2016 at 13:36
  • what is rssFeedService? Is this an angular service or a third party library's function ? Commented Mar 29, 2016 at 13:41
  • @S4beR, It's http.get function Commented Mar 29, 2016 at 13:57

1 Answer 1

1

If you are using ng-repeat then I would recommend 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.

Sign up to request clarification or add additional context in comments.

4 Comments

q.isRead determine true or false, I've tried your example, but it didn't set color on it
@Vitaliy you set a class on your element.
if I understood you correctly, I set this class on q.link or q.body and it did not change colors
Going by your example, you should be able to replace your h1 tag with my one. You should create the classes .green and .red as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.