Ashish
Ashish

Reputation: 479

Angular : how to access HTML tag Class in Typescript

I am trying to access the class names of Form and its controls in the Typescript file. Is it possible? Please check the Code : https://stackblitz.com/edit/angular-ivy-ps9aeb

ngOnInit(): void {
    console.log(this.ModelForm.form.controls.firstName?.valid);
  }

The above code is giving error

Error in src/app/app.component.ts (11:22)
Property 'ModelForm' does not exist on type 'AppComponent'.

Upvotes: 0

Views: 977

Answers (1)

Jason White
Jason White

Reputation: 5813

You need to use @ViewChild to have access to the template driven form in your typescript file.

export class AppComponent implements OnInit {
  @ViewChild('ModelForm') 
  public ModelForm: NgForm;
  //...
}

https://stackblitz.com/edit/angular-ivy-ypsta6

Upvotes: 2

Related Questions