AngularJS $promise then() data undefined

后端 未结 4 702
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 10:24

I am trying to get data assigned to a $scope variable. Inside my $promise.then() function it is displaying correctly but outside the function it shows as undefined. The foll

4条回答
  •  無奈伤痛
    2021-01-05 11:10

    This code will help you to fetch data and resolve with promise, please note fetch data will only work after then call...

    getData(){
          const promise = this.httpClient.get(this.PHP_API_SERVER+'/api/books').toPromise();  
          promise.then((data)=>{
            console.log("Resolved: " + JSON.stringify(data));
            return JSON.stringify(data);
          }, (error)=>{
            console.log("Rejected " + JSON.stringify(error));
          })
        }
    

提交回复
热议问题