Search code examples
angularangular2-formsangular2-httpangular2-formbuilder

Failed to pass array to child component


I want the Dynamic Forms Cookbook example to work with http service. The error is TypeError: Cannot read property 'forEach' of undefined where the undefined is regarding array I'm passing to child component.

Feel free to look it into plunker. I've made a change to original example adding Http service which get data from json file.

What I do wrong here?


Solution

  • questions has to be initialized, and ngOnChanges has to used. AppComponent

    this.questions = [];
    

    DynamicFormComponent

    ngOnChanges() {
      this.form = this.qcs.toFormGroup(this.questions);
    }
    

    See http://embed.plnkr.co/fHXC0T/