user2146414
user2146414

Reputation: 1038

Angular ReactiveFrorm: Should setValue trigger ngModelChange

I have the following code:

<form [formGroup]="form" (ngSubmit)="register()">
    <div>
        <input type="text" formControlName="username" (ngModelChange)="updateErrors('username')" />
    </div>

    <div>
      <button size="large" type="button" (click)="form.get('username').setValue('User')">Set username</button>
    </div>
</form>

When I click the "Set username" button the value of username changes but the updateErrors method is not called.

Should it be? Maybe it should not but I do not understand something about ngModelChange event... (?)

Upvotes: 0

Views: 584

Answers (1)

Marc
Marc

Reputation: 1896

You should not mix ngModel and reactive forms. When using forms, you can listen on changes with

  this.form.valueChanges
     .subscribe(evt => {
        console.info(evt)
     });

Don't forget to unsubscribe: 6 Ways to Unsubscribe from Observables in Angular

Upvotes: 2

Related Questions