Home > database >  Check if native element exist or not in Angular
Check if native element exist or not in Angular

Time:07-11

I have this checkbox

  <input #optionA  type="checkbox" name="answer" value="opt_a" />

  <input #optionB  *ngIf="some condition" type="checkbox" name="answer" value="opt_b" />

in ts file

 @ViewChild("optionA") optionA : ElementRef;
  @ViewChild("optionB") optionB: ElementRef;

method()
{
  this.optionA.nativeElement ? console.log(this.optionA.nativeElement.checked) : '';
  this.optionB.nativeElement ? console.log(this.optionB.nativeElement.checked) : '';

getting error if nativeElement not available in this case optionB checkbox not generated is there anyway to check if the element exist.

CodePudding user response:

optionA / optionB are probably undefined. Check for them.

  • Related