简体   繁体   中英

How to store http result in angular

couldn't find answers from other questions.

I'm trying to get data from a web service, and then draw a chart and display a table below it.

If I put the datatable inside the .then function it throws in an error. So what I'm thinking is to store the result in a json variable and load it using fromSource .

The problem right now is I can see the def.promise , but when I try to access the .value, it shows 'undefined'.

any idea?

app.controller('abc', function($scope, $q, $http){
 var def = $q.defer();

$http.get(API).then(function(result){
  def.resolve(result);
  //draw a chart
});
console.log(def.promise.$$state.value); //returns undefined. 
console.log(def.promise); 
//returns:
//d {$$state: Object, then: function, catch: function, finally: function}$$state: Objectstatus: 1value: Array[39]0: //Object1: Object2:

 $scope.dtOptions = DTOptionsBuilder.fromSource(angular.toJson(def.promise.$$state.value));
});

Okay, I figured it out...

Instead of using fromSource for referencing data for the datatable, you can use fromFnPromise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM