Is there any way I can pass dynamic data to a component in Angular?

前端 未结 4 1849
抹茶落季
抹茶落季 2021-01-12 20:42

I am trying to pass data that is dynamic to a child component. But I always get the data as undefined in the child component. Below is what I am doing.

ParentComp

4条回答
  •  走了就别回头了
    2021-01-12 21:07

    Why not using the Observable with the async pipe. If you want to console-log that value use a setter. The async pipe will also take care about unsubscribing.

    results: Observable;
    ngOnInit() {
      this.results = this.http.get('url');
    }
    

    In the HTML

    
    

    And in your child component

    @Input('dataNeeded') 
    set dataNeeded(val: any[]) {
      console.log(val);
    }
    

提交回复
热议问题