Oskar
Oskar

Reputation: 482

Changing class of element in Angular 4

Hello like in the subject, I want to change the class of the item like this:

<a class="nav-link active" href="#"> --> <a class="nav-link" href="#">

Have you any ideas? Thanks for ur answer.

Upvotes: 0

Views: 305

Answers (3)

fiveobjects
fiveobjects

Reputation: 4269

I am assuming you are using a condition to decide the classes you are going to apply to the element. You may use ngClass directive to decide the classes to apply:

<a [ngClass]="{'nav-link active' : condition1, 'nav-link': condition2}" href="#">
Click here
</a>

The above is an example only. You may use this example to write to evaluate different expressions/conditions and apply one or more CSS classes.

Upvotes: 1

Javier Mart&#237;nez
Javier Mart&#237;nez

Reputation: 356

You can use ngClass directive:

<a class="nav-link" [ngClass]="{'active' : condition}" href="#">

Upvotes: 1

chrnx
chrnx

Reputation: 533

You can use the [class.] directive provided by angular like so:

<a class="nav-link" [class.active]"your-condition" href="#">

You can read more about this here: https://angular.io/guide/attribute-binding#binding-to-the-class-attribute.

Upvotes: 2

Related Questions