Shinji035
Shinji035

Reputation: 361

bind to ngModel but got "undefined" ( Angular 8)

I have bound a property to ngModel , but it always gave me undefined

  <div>
            <input
              type="radio"
              name="input-alumni"
              id="input-alumni-2"
              value="true"
              [(ngModel)]="isAlumni"
              (change)="handleAlumiSelect($event)"
              required
            />
            <label for="input-alumni-2">&nbsp;Yes</label>
          </div>
<div>
            <input
              type="radio"
              name="input-alumni"
              id="input-alumni-1"
              value="false"
              [(ngModel)]="isAlumni"
              (change)="handleAlumiSelect($event)"
            />
            <label for="input-alumni-1">&nbsp;No</label>
          </div>

I print it in handleAlumiSelect:

public isAlumni: any;
  handleAlumiSelect(e: any) {
    console.log('handleAlumiSelect');
    console.log(e.target.value);
    console.log(this.isAlumni);
  }

e.target.value shows me the right anwser, but this.isAlumni is always ("undefined").

can any one tell me why?

Upvotes: 0

Views: 4385

Answers (2)

Shinji035
Shinji035

Reputation: 361

Finally, it's nothing wrong about my code. there are some other errors in my html file , because I'm updating the project form an AngularJS project. those errors maybe affected the binding. So I just remove all codes that lead to errors. and the ngmodel works.

Upvotes: 0

Seba Cherian
Seba Cherian

Reputation: 1793

Do not use value with ngModel, remove it first.

 <input type="radio" name="input-alumni" id="input-alumni-1" [(ngModel)]="isAlumni" (change)="handleAlumiSelect($event)"/>

Upvotes: 0

Related Questions