Bolboa Bolboa - 2 months ago 17
AngularJS Question

angular check is class is odd or even on ng-click

I have the following ng-repeat that gives an element a className based on whether it is even or odd...

<div ng-click="displayHTML(content)" ng-class-odd="'title'" ng-class-even="'html'" ng-repeat="content in name.name">
{{content}}
</div>


On
ng-click
I am calling
displayHTML()
and passing a parameter
content
so that only that particular div that is clicked calls the function.

However on ng-click I'm attempting to see whether the clicked element is
ng-class-odd
or
ng-class-even
and I want the function to only be called if the element is
ng-class-odd
.

But I do not know an easy way to do this. Is there an easy "angular" way of doing it. If not, what should I put here...

$scope.displayHTML = function(obj){
////

}

Answer

ngRepeat exposes several local variables to the scope, specifically $even and $odd. Just use those:

<div ng-click="displayHTML(content, $even)" ng-class-odd="'title'" ng-class-even="'html'" ng-repeat="content in name.name">
    {{content}}
</div>

js:

$scope.displayHTML = function(obj, $even) {
    if($even) {/* even code*/}
    else {/* odd code */}
}
Comments