How to pass data from parent to child component in Angular 4

后端 未结 4 1778
一个人的身影
一个人的身影 2021-02-01 04:10

When I try to pass data from parent to child component. I\'m getting undefined message in the console. Data is in the form of array.

parent.component.html

4条回答
  •  既然无缘
    2021-02-01 04:50

    You can't do the assignment in the constructor as the value has not yet been populated, it should be done in ngOnInit just like your check of the value.

    @Input() data;
    question = [];
    
    constructor() {
    }
    
    ngOnInit() {
      this.question = this.data;
      console.log(this.question);
    }
    

提交回复
热议问题