Harsh Mendapara
Harsh Mendapara

Reputation: 313

How can I remove an error from my HTML file, while implementing the method in ngClass?

I don't know where I'm getting wrong.

  1. app.component.html

    <div [ngClass]="stylemethodName();"> ngClass with method </div>
    
  2. app.component.ts

    export class AppComponent {
    title = 'Simple_CRM_App';
    
     stylemethodName(){
    
        return 'c3';
    
      }
    }
    
  3. app.component.scss

     .c3 {
            color:red;
      }
    

I'm getting an error in the template app component. Error

Upvotes: 0

Views: 354

Answers (1)

NeNaD
NeNaD

Reputation: 20354

Remove the ; from the expression:

<div [ngClass]="stylemethodName()"> ngClass with method </div>

Also, it is really bad practice when you are returning values from the function. That function will be triggered on each Change Detection. Try to implement a logic in template directly.

Upvotes: 1

Related Questions