本文介绍了角度8:PatchValue无法与ChangeDetector和UpdateValue一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么PatchValue无法与FormBuilder一起使用.检索值时显示数据,但未在FormBuilder中设置.有人知道为什么吗?我正在使用UpdateValue和Changedetector.请参见下面的调试器图片,设置硬编码值也不起作用

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

更新:

找到答案,看看Formbuilder,它有多余的冒号或空格,猜测的实际问题是,许多人编写了100个组件,当有轻微的字符问题或空格时,如何防止这种情况再次发生,等等.?

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?

地址组件:

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();
  }

调试器图片

注意:它从@Output接收addressPurposeEvent,并从下拉菜单中获取值,并发送给Parent,

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'}});

推荐答案

您应该尝试像这样更新

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

这将为您提供该组的控件,并且您可以使用其patchValue,以您认为我的角度不会得到更新的方式.希望对您有帮助!

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!

这篇关于角度8:PatchValue无法与ChangeDetector和UpdateValue一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:04