0

i am bigginer in angular,my form in html is :

<mat-card  style="padding:20px;margin:30px;text-align: right;">
  <mat-card-header>
    <mat-card-title>title</mat-card-title>
  </mat-card-header>
   <mat-card-content style="width: 150px; display: contents;" [formGroup]="formGroupContent">
          <div>
          <mat-form-field class = "tp-full-width" style="padding-left: 20px;">
              <input #amvalcode formControlName="equiid" matInput placeholder = "code" value = "">
          </mat-form-field>
        </mat-card-content>
  <mat-card-actions >
          <button mat-raised-button color="accent" type="button" style="width:200px;" 
          (click)="add_equi(captioncode.selected.value.id)">add</button>
  </mat-card-actions> 

and in type script :

    add_equi(CaptionCode){
        if (this.formGroupContent.controls['equiid']?.value === undefined){
                    console.log("undefined value");
        }else{
                    console.log("has value");
         }
    }

equiid is formControlName ,but when in equiid is null i recive

Cannot read property 'value' of undefined
mahdi.gh
  • 71
  • 1
  • 2
  • 9
  • Simply check if this.formGroupContent.controls['equiid'] is null? – Romka Jun 07 '21 at 10:48
  • Does this answer your question? [Is there a way to check for both \`null\` and \`undefined\`?](https://stackoverflow.com/questions/28975896/is-there-a-way-to-check-for-both-null-and-undefined) – Yong Shun Jun 07 '21 at 10:56
  • 1
    The error is not showing the line it's been dores? Also, captioncode.selected in your HTML is defined? – Felipe Bonfante Jun 07 '21 at 11:07
  • yes ,it work where has value but when no value in control i recive error – mahdi.gh Jun 07 '21 at 12:47

0 Answers0