Hi, I have a scenario where I load dropdown options and then get the value for the dropdown . When the value is patched prior to loading the options, the patched value is ignored
I've included an example with a replay subject observable
https://stackblitz.com/edit/angular-snprkn-4et8y2?file=app.component.ts
Expected behavior : Dropdown should be set to patched value "A"
Actual behavior : Dropdown is set to initial value. "C"
I've also included another example with a normal observable
https://stackblitz.com/edit/angular-snprkn-mffhc7?file=app.component.ts
code sample
constructor(private fb: FormBuilder) {
console.log('Make Form');
this.form = this.fb.group({
MyDropDown: 'C',
});
window.setTimeout(() => {
this.form.patchValue({
MyDropDown: 'A',
});
console.log('Patch Value');
}, 2000);
window.setTimeout(() => {
let arr: OptionDto[] = [];
arr.push(<OptionDto>{ label: '', code: '' });
arr.push(<OptionDto>{ label: 'A Label', code: 'A' });
arr.push(<OptionDto>{ label: 'B Label', code: 'B' });
arr.push(<OptionDto>{ label: 'C Label', code: 'C' });
arr.push(<OptionDto>{ label: 'D Label', code: 'D' });
this.MyDropDownOptions.next(arr);
console.log('Set options');
}, 2000);
}
|
I guess I'm trying to report a bug and heres why.
An observable may be resolved at any time - before or after the value of the dropdown is set. That is why its an observable. The dropdown documentation indicates it has support for observables.
A reactive form can be patched at any time after initialization.
The patching of the form and the completion of the observable are independent and I don't think the programmer should have to synchronize them. So, the code in the samples in the initial post are valid, but, the control does not work as I believe it should.
Let me know if this is the proper channel to report a bug or if I should report it elsewhere.
Have you made any progress?
Thanks,
Sam
constructor(private fb: FormBuilder) {
console.log('Make Form');
let val: any;
this.form = this.fb.group({
MyDropDown: 'B',
});
this.form.patchValue({
MyDropDown: 'D',
});
window.setTimeout(() => {
this.form.patchValue({
MyDropDown: 'A',
});
console.log('Patch Value');
}, 1500);
window.setTimeout(() => {
let arr: OptionDto[] = [];
arr.push(<OptionDto>{ label: '', code: '' });
arr.push(<OptionDto>{ label: 'A Label', code: 'A' });
arr.push(<OptionDto>{ label: 'B Label', code: 'B' });
arr.push(<OptionDto>{ label: 'C Label', code: 'C' });
arr.push(<OptionDto>{ label: 'D Label', code: 'D' });
this.MyDropDownOptions.next(arr);
console.log('Set options');
}, 3000);
}
}
|
In the examples above, setTimeout represents an asynchronous call. In actual application code, this would be a http request to the server for more data, or a proper value. Both the form values and the drop down options are queried for asynchronously and may arrive in either order. Thus, if the form values arrive first, the patch value operation occurs before the drop down data source gets loaded.
The issue as described above is not fictitious. It happens on every screen in our data entry application when we get the data source and form values in a separate http call. It is highly relevant to our continued use of the drop down control.
Thanks for your response and I look forward to hearing how you are going to solve this issue.