baliman
baliman

Reputation: 620

Angular 6 : Can not load dynamic form

I want to dynamic load forms but when I click on the button to load the second form I get an error.

As I understand @Viewchild sets the fields property in my child component but does not update that when clicking the button to load the second form.

See also the loadForm method in the app-component.

ERROR Error: Cannot find control with name: 'userName'

I have a working example in stackbliz.

https://angular-y3n2u9.stackblitz.io

view the console log to see the error message

Anyone has an idea why the second form is not loaded in my demoform-component.

Thanks

John

Upvotes: 0

Views: 107

Answers (1)

Chellappan வ
Chellappan வ

Reputation: 27471

All primitive types are passed by value. Objects, arrays, and functions are also passed by value so the change detection is not being fired when you load form2 try ngOnChanges life cycle hook it will work.

    ngOnChanges(){
         this.dynamicForm = this.createForm();
    }

Example:https://stackblitz.com/edit/angular-y3n2u9

Upvotes: 1

Related Questions