Ole Albers Ole Albers - 3 months ago 7
AngularJS Question

What is the right way for a parameter-condition in ngRepeat?

I want to change the class of an element according to aproperty of an ngRepeat-value. BOTH of the following variants work.

SearchCategory is a string within the $scope, Categories is an Array of Objects within $scope.

Variant 1:

<li ng-repeat="category in Categories"
ng-class="SearchCategory==category.name?'active':''" >
some text
</li>


Variant 2:

<li ng-repeat="category in Categories"
ng-class="SearchCategory=='{{category.name}}'?'active':''" >
some text
</li>


The difference is that in devmode (F12) in the second variant the Category is replaced. As both variants seem to work, which one is the "better" or suggested method?

Answer

this is probably the best, and most conventional:

<li ng-repeat="category in Categories" ng-class="{'active':SearchCategory===category.name}">
    some text
</li>
Comments