How to make form model update on blur in angular 2

前端 未结 2 443
别那么骄傲
别那么骄傲 2021-01-01 18:56

Is there an equivalent to this in angular 2?

ng-model-options=\"{ updateOn: \'blur\' }\"

Thanks

相关标签:
2条回答
  • 2021-01-01 19:23

    In Angular 2 you can use the native DOM events

    <input (blur)="someMethod()" />
    

    Now, just define a method that does what you need when the field is blurred

    0 讨论(0)
  • 2021-01-01 19:37

    Even though this is a very old thread, there is now a very neat solution which comes with Angular5.

    You trigger the update on blur like this:

    Tempalte driven forms:

    <input [(ngModel)]="lastname" [ngModelOptions]="{ updateOn: 'blur' }">
    

    Reactive forms:

    this.nameForm = new FormGroup ({
      firstname: new FormControl('', {
        validators: Validators.required,
        updateOn: 'submit'
      }),
      lastname: new FormControl('', {
        validators: Validators.required,
        updateOn: 'submit'
      })
    });
    

    (you can select submit or blur as values)

    Reference: https://medium.com/codingthesmartway-com-blog/angular-5-forms-update-9587c3735cd3

    0 讨论(0)
提交回复
热议问题