My template includes the following:
@Component({
standalone: true,
imports: [DynamicFormComponent, NgForOf, NgIf, NgSwitch, NgSwitchCase, NgTemplateOutlet],
template: `
<ng-container [ngSwitch]="method">
<ng-container *ngSwitchCase="file">
<ng-container *ngIf="!isFileUploaded; else form">
...some html...
</ng-container>
</ng-container>
<ng-container *ngSwitchCase="manual">
<ng-container *ngTemplateOutlet="form"></ng-container>
</ng-container>
</ng-container>
<ng-template #form>
<app-dynamic-form></app-dynamic-form>
</ng-template>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild(DynamicFormComponent, { static: false })
public form!: DynamicFormComponent;
private createForm(): void {
// method to run after DynamicFormComponent render...
const fields: any[] = this.createFields();
this.form.setFields(fields);
}
createFields(): any[] {
return [/*fields...*/]
}
}
I am trying to set a variable inside <app-dynamic-form>
using its setFields()
method.
export class DynamicFormComponent {
public fields: any[];
public setFields(val: any[]): void {
if (val) {
this.fields = val;
}
}
}
An issue arises when this component is rendered later via ng-template
than when the parent is initialized. I have attempted to do it in ngAfterViewInit()
without success, and also tried emitting the event in ngOnInit()
with no luck either.