Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

typescript - Angular 8: PatchValue not working with ChangeDetector and UpdateValue

I am trying to figure out why PatchValue does not work with FormBuilder. It shows data when retrieving the value, but does not set in the FormBuilder. Anyone know why? I am using UpdateValue and Changedetector. See Debugger picture below, Setting hard coded values does not work either

Update:

Found the answer, look at the Formbuilder, it has extra colon or space, guess the real question is, many have written 100 of components, how does one prevent this from happening again when there are slight character issues or spaces, etc?

Address Component:

this.editSharedForm = this.formBuilder.group({
  'phoneNumber': [null, [Validators.required, Validators.maxLength(50)]],
  'emailAddress': [null, [Validators.required, Validators.maxLength(50), Validators.email]],
  'effectiveStartDate': [null, [Validators.maxLength(200)]],
  'addressChangeReason': this.formBuilder.group({
    'addressChangeReasonId: : ': [null, [Validators.required]],
    'addressChangeReasonCode: : ': [null, [Validators.required, Validators.maxLength(50)]],
    'addressChangeReasonDescription: : ': [null, [Validators.required, Validators.maxLength(255)]]
  }),
  'addressPurpose': this.formBuilder.group({
    'lkPurposeofUseId: ': [null, [Validators.required]],
    'purposeofUseCode: ': [null, [Validators.required, Validators.maxLength(50)]],
    'purposeofUseDescription: ': [null, [Validators.required, Validators.maxLength(255)]]
  })

  addressPurposeChangeEvent(addressPurposeEvent) {
    this.cdr.detectChanges();
    this.editSharedForm.updateValueAndValidity();
    this.editSharedForm.patchValue({ addressPurpose : addressPurposeEvent });
    this.cdr.detectChanges();
    this.editSharedForm.updateValueAndValidity();
    this.cdr.detectChanges();
  }

Debugger Picture

Note: It receives addressPurposeEvent from an @Output, its a dropdown menu with values, and its send to the Parent ,

<app-address-purposeofuse-dropdown (addressPurposeChange)="addressPurposeChangeEvent($event)"></app-address-purposeofuse-dropdown>

Update: Setting hard values does not work either

this.editSharedForm.patchValue({ addressPurpose : { purposeofUseCode: 'test'}});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should try to update like this

this.editSharedForm.get('addressPurpose').patchValue(addressPurposeEvent);

this will get you the control for the group and you can use its patchValue, with your way i dont think angular gets what to update. Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...