puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

patchValue and setValue in Angular #100

Open puddlejumper26 opened 4 years ago

puddlejumper26 commented 4 years ago

image

image

Demo

inside this demo, if we set

updateProfile() {
    this.profileForm.patchValue({     <====
      firstName: 'dddddd',
      lastName: '3333',
      address: {
        street: '123 Drew Street',
        city:'Mmmm'
      }
    });
  }

than the browser would be like this image

if we set like this

updateProfile() {
    this.profileForm.setValue({
      firstName: 'dddddd',
      lastName: '3333',
      address: {
        street: '123 Drew Street',
        city:'Mmmm'
      }
    });
  }

image

Notice above the input are empty, and the Error is : Must supply a value for form control with name: ***.

Reference

[1] https://angular.io/guide/reactive-forms#patching-the-model-value