3

I have a code on which I have used class binding. When the button is clicked, the color of the font should change based on the value of textrun. textrun changes between true and false.IF true, it should display text in red color else in green color.

TS File

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  textrun=true;
  messageClasses={
    "text-success": !this.textrun,
    "text-error": this.textrun,
    "text-info": !this.textrun

    }

  changetrue(){
  this.textrun=false;
  console.log("done");
}
}

HTML File

 <h2 [ngClass]="messageClasses">hai</h2>
  <button (click)="changetrue()">click</button>

css File

.text-success{
  color:green;
}
.text-error{
  color:red;
}
.text-info{
font-style: italic;
}

EDIT: I needed the same code to work if I have multiple conditions to be applied.

4 Answers 4

3

You can use any of the below approach


Approach 1:

<h2 [ngClass]="{'text-error': textrun', 'text-success': !textrun }">hai</h2>

Approach 2:

<h2 [ngClass]="textrun ? 'text-error':'text-success'">hai</h2>

Approach 3:

<h2 [ngClass]="{true:'text-error',false:'text-success'}[textrun]">hai</h2>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @Surjeet. But if I have three conditions to be applied. Like I need the font to be italics also, how do I apply class binding?
Thank you . I got it. Appreciate you Support.
the last approach is a ninjs style 🐱‍👤 👍
😃 @MuhammedAlbarmavi
2

Try like this:

<h2 [ngClass]="textrun ? 'text-error':'text-success'">hai</h2>

or,

<h2 [ngClass]="{'text-error': textrun, 'text-success': !textrun }">hai</h2>

Demo

3 Comments

Thank you @Adrita. It sure can be written that way. And it works. I had tried. But I don't understand why the above code doesn't work. Any suggestion on that ?
@nXn The syntax you were trying was incorrect. I have added another way to do it in the demo
Thank you. Please update the answer with the answer in Demo so that other can also make use of the info. That worked.Appreciate you Support.
2

This won't work since the messageClasses is loaded only once, during the class instantiation.

Change your html to:

<h2 [ngClass]="{
  "text-success text-info": !textrun,
  "text-error": textrun
  }">
  hai
</h2>
<button (click)="changetrue()">click</button>

and delete your messageClasses variable from your class

1 Comment

Yes. Thank you. I got it.
1

It does not work, because you are refering to this.textrun on your attributes.

Usually it is not available when the component is initialized:

Try:

textrun = true;

messageClasses;

ngOnInit() {
   this.messageClasses = {
    "text-success": !this.textrun,
    "text-error": this.textrun,
    "text-info": !this.textrun
   }
}

onClick() {
  this.textrun = !this.textrun;
  this.messageClasses = {
    "text-success": !this.textrun,
    "text-error": this.textrun,
    "text-info": !this.textrun
  }
}

At ngOnInit() all attributes should be declared and initialized.

3 Comments

A brief explanation what was not working would much more helpful instead of 'not working'
Can you log this after that?
i made the changes you mentioned and the color isn't changing on button click

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.