jaylani
jaylani

Reputation: 49

How to add class in a div if the year is current year in angularjs

I have my div like the following...

<div class="slider-slide-wrap current_year shown slide{{yr}}" ng-repeat="yr in yeardata"></div>

in {{yr}} I am getting stored year data.Here I need to check the condition If {{yr}} == {{currentyear}} then I need to add the classes 'shown' and 'current_year ' to the div .

How is this possible??

Upvotes: 0

Views: 55

Answers (3)

Geo Jacob
Geo Jacob

Reputation: 6009

Use ng-class

ng-class="{'clasName':yr==currentyear}"

Upvotes: 0

Arman Mukatov
Arman Mukatov

Reputation: 140

you can use directive ng-class

<div **ng-class={'shown current_year': yr == currentyear, }** class="slider-slide-wrap current_year shown slide{{yr}}" ng-repeat="yr in yeardata"></div>

Upvotes: 1

Gabriel Pires
Gabriel Pires

Reputation: 366

You should use ng-class (link to the docs: https://docs.angularjs.org/api/ng/directive/ngClass)

You can set a class based on a condition with it, using it like this:

<div ng-class={ 'your-class': year === currentYear }></div>

The ng-class argument is a dictionary, so you can set multiple classes with it (like you wanted to), using it like:

<div ng-class={ 'your-class': year === currentYear, 'your-second-class': year === currentYear }></div>

Upvotes: 0

Related Questions