Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-class with different conditions

i have a view which shows data and i want to add another class on the list items in my view.

<input type="text" class="filter" placeholder="search..." ng-model="search">
<ul>
    <li ng-repeat="item in items | filter:search | orderBy:'date'">
        {{ date }} {{ item.heading }}<button ng-click="deleteItem(item.ID)"><i class='icon-trash'></i></button>
    </li>
</ul>

i have a variables called item.date and i want to compare it to another variables today. Here is the logic:

if (item.date - today <= 0) 
    apply class1 
if else (item.date - today > 0 && item.date - today <= 3)
    apply class2
and so on

how can i achieve this with angular? can i put this logic directly into my view or do i need to define it in my controller?

thanks in advance

like image 895
arkhon Avatar asked Dec 02 '22 22:12

arkhon


1 Answers

Since you have a bit heavy comparisons to make, I would suggest moving it inside a function rather than having it in HTML:

<li ng-class="getClass(item.date)" 
    ng-repeat="item in items | filter:search | orderBy:'date'">

JS:

$scope.getClass = function(date){
    /* comparison logic here*/
    /* returs class name as string */
}
like image 137
AlwaysALearner Avatar answered Dec 05 '22 10:12

AlwaysALearner