Unable to load data inside ngOnInit

前端 未结 4 868
感情败类
感情败类 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:41

    The getStudentList method of the service returns an Observable therefore it is an async function. So data => {this.items = data} line performs only after the subscription is completed. But this.items is not undefined since you assign it an empty array with items: any = [], it is only empty.

    You can log the values with buttonClicked method because your http request has been completed.

    I think you need to get more familiar with Observable from RxJS.

    Here is the official docs of HttpClient: https://angular.io/guide/http

提交回复
热议问题