Error with TreeSelect and a two-level list of options #4873
Replies: 1 comment
|
The two tree levels are not the problem. The binding writes the wrong runtime shape into the Signal Forms model. Your field is a That changes import { TreeNode } from "primeng/api";
import { FieldTree } from "@angular/forms/signals";
field = input.required<FieldTree<string>>();
selectedSector: TreeNode | null = null;
selectSector(event: { node: TreeNode }): void {
const key = event.node.key;
if (key != null) {
const targetField = this.field();
targetField().value.set(String(key));
}
}<p-treeselect
[options]="activitySectors"
[(ngModel)]="selectedSector"
[ngModelOptions]="{ standalone: true }"
(onNodeSelect)="selectSector($event)"
[placeholder]="placeholder()"
fluid
/>Remove If this component also edits existing data, initialize |

The two tree levels are not the problem. The binding writes the wrong runtime shape into the Signal Forms model.
Your field is a
string, but PrimeNG TreeSelect emits the selected TreeNode object: the Tree sets its single selection tonode(source), and TreeSelect passes that object directly to its ControlValueAccessor callback (source). Thedata: "general-masonry"property does not make TreeSelect emit that string.That changes
activitySectorfrom a string leaf into a TreeNode object while Angular is maintaining its field tree, which explains the recursive signal stack. Keep the form model primitive and adapt the TreeSelect value: