Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular, Javascript, PHP

This is the html file and the code

<ul class="list">
    <li id="numword" data-score="{{item.score}}" class="" ng-repeat="item in words track by $index">
        {{item.word}} {{item.score}}
    </li>
</ul>

This is how it looks in html

<li>nice 0.4</li>
<li>sad -0.2</li>
<li>modest 0</li>

This is the js file in angular

$http.get('select.php') .success(function(data){ $scope.words = data; })

This is the database

I have value of words and scores, every word has own score that is in number

My question is: data-score="{{item.score}}" i got values of 0.4, 0.2, -0.1, 0...

I need to check if value is greater than 0, equal to 0 and less to 0 in order to add classes to li.

How can i do that ?

like image 285
Jovan Poplasen Avatar asked Feb 11 '26 13:02

Jovan Poplasen


1 Answers

You need ngClass attribute for this:

ng-class="{positive: item.score > 0, negative: item.score < 0}"

This directive is using an object, with property as the class and value as the condition. If the condition is true, then the class (which is the property) will be added to the element.

Read more about ngClass here: https://docs.angularjs.org/api/ng/directive/ngClass

like image 97
Alon Eitan Avatar answered Feb 14 '26 03:02

Alon Eitan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!