Angular 5+ cannot save result form observable response

前端 未结 4 1700
刺人心
刺人心 2021-01-25 00:58

I have an Angular app which consumes data from a webapi. When i make a request then i get an typed observable.

now when i do this:

data: Product[];
produ         


        
4条回答
  •  离开以前
    2021-01-25 01:42

    You are working with async programming you cannot pause the execution of the code and your subscription will be resolved in future but you cannot predict when. console.log() outside the subscribe is executed before your subscription is resolved
    what you can do is You can store the value in a class property inside subscribe callback .Refer this for better understanding.

       data: Product[];
    productService.getByProductId("061000957").subscribe(res => {
           console.log(res);
           this.data = res;
           console.log(this.data);
        });
    

提交回复
热议问题