Unable to load data inside ngOnInit

前端 未结 4 842
感情败类
感情败类 2021-01-26 10:31

My goal is to load a dropdown when a component renders. I have a service which hits a url and gets json data. Something like this

 @Injectable()
 export class St         


        
4条回答
  •  無奈伤痛
    2021-01-26 10:44

    Remove this assignment this.items = this.studentListService.getStudent().... You are trying to assign Subscription object to this.items.

    ngOnInit() {
           this.studentListService.getStudent().subscribe(
               data => {this.items = data},
               err => console.error(err),
               () => console.log('Done Loading Student Data'));
    
    }
    

    Note : You can only access it once the http (asynchronous) call is completed. And when it is completed, your dropdown should automatically load those items. If you try to print it outside subscribe block, it gives you undefined.

提交回复
热议问题