Angular 如何使用反应式表单valueChanges debounce进行首次保存,而无需通过修补id进入循环
我正在尝试对表单字段的输入进行去抖动,如果表单是新的,并且在我创建并从数据库接收记录时不包含ID,我需要用新值修补表单。{id,updatedAt etc},但是,当使用valueChanges debounce订阅时,这会导致一个无限循环,因为每次修补表单时,valueChanges都会再次触发 我可以在表单输入上使用Angular 如何使用反应式表单valueChanges debounce进行首次保存,而无需通过修补id进入循环,angular,Angular,我正在尝试对表单字段的输入进行去抖动,如果表单是新的,并且在我创建并从数据库接收记录时不包含ID,我需要用新值修补表单。{id,updatedAt etc},但是,当使用valueChanges debounce订阅时,这会导致一个无限循环,因为每次修补表单时,valueChanges都会再次触发 我可以在表单输入上使用(input)=“saveClient(clientForm)”,但我不知道如何使其去抖动 /** * On init * * @returns {v
(input)=“saveClient(clientForm)”
,但我不知道如何使其去抖动
/**
* On init
*
* @returns {void}
*/
public ngOnInit (): void {
this.clientForm = this.clientService.getForm({})
this.subscriptions.push(
this.clientForm.valueChanges
.debounceTime(1000)
.distinctUntilChanged()
.subscribe((data) => {
this.saveClient(this.clientForm)
})
)
this.display = true
}
/**
* Save client
*
* @param {FormGroup} clientForm Client form
*
* @returns {void}
*/
public saveClient (clientForm: FormGroup) {
return this.clientService.api.save(clientForm.value)
.toPromise()
.then((result) => {
clientForm.patchValue(result)
return result
})
}
您需要在不发出事件的情况下修补该值。像这样:
clientForm.patchValue(result, { emitEvent: false });
如果emitEvent为true,此更改将导致FormControl上的valueChanges事件被发出。这默认为true(因为它取决于updateValueAndValidity)
您需要在不发出事件的情况下修补该值。像这样:
clientForm.patchValue(result, { emitEvent: false });
如果emitEvent为true,此更改将导致FormControl上的valueChanges事件被发出。这默认为true(因为它取决于updateValueAndValidity)
非常感谢,我没有想到检查patchValue是否有选项。很高兴我能提供帮助。非常感谢,我没有想到检查patchValue是否有选项。很高兴我能提供帮助。