ng-matero / extensions

Angular Material Extensions Library.
https://ng-matero.github.io/extensions/
MIT License
388 stars 48 forks source link

[MtxSelect] Error when reinit formgroup #293

Closed andreanaFelicione closed 1 month ago

andreanaFelicione commented 1 month ago

When reinitialize the formGroup, for example re-executing the following

this.form = this.fb.group({....})`
from the second time onwards, the mtxSelect stop to work and shows this error in console:
`ERROR Error: There is no FormControl instance attached to form control element with name: 'test'
    at _throwError (forms.mjs:3162:11)
    at _noControlError (forms.mjs:3158:12)
    at MtxSelect.noop (forms.mjs:2976:13)
    at set value (mtxSelect.mjs:232:14)
    at MtxSelect.writeValue (mtxSelect.mjs:447:19)
    at setUpControl (forms.mjs:2950:23)
    at forms.mjs:4914:21
    at Array.forEach (<anonymous>)
    at FormGroupDirective._updateDomValue (forms.mjs:4901:25)
    at FormGroupDirective.ngOnChanges (forms.mjs:4729:18)

I have @angular/material@17.3.1 and @ng-matero/extensions@17.1.3

nzbin commented 1 month ago

Please give me an online example

andreanaFelicione commented 1 month ago

Here is a test project: testStackBlitz

nzbin commented 1 month ago

I can't find issues about mtx-select in your demo. But I'd like to point out a few irregularities. If you want to init the form in ngOnInit hook, you should define the form like this.

- form!: FormGroup
+ form?: FormGroup

But I think the best way to init Reactive Form should like this.

form = this.fb.group({
  test: 1
});

ngOnInit(): void {
  this.form.patchValue(...)
}
andreanaFelicione commented 1 month ago

Clicking the button the select should set value to "Setted Value", instead it remain with "Default Value" and shows the error "There is no FormControl instance attached to form control element with name: 'test'" in the console. How can you say you don't find any issues? This is the only component that behaves like this, all other material inputs do not cause problems with reinit of the formGroup.

PS. My entire project of thousands and thousands of lines is managed with reinitialized formGroups, the idea of ​​modifying everything using patchValue is absolutely unthinkable.

image

nzbin commented 1 month ago

I'm sorry, it's indeed a problem, I'll try to fix it.

nzbin commented 1 month ago

The new version 17.1.4 has fixed this issue, please try again.

andreanaFelicione commented 1 month ago

Perfect. It works now, thank you so much!