ng-csv包含来自api调用的数据

Nam*_*Roy 6 angularjs

我需要从api调用中为ng-csv分配数据.我试过这个.但没有奏效

script:
$scope.getArray = function () {

    var temp;
    $http.get(Config.serviceUrlBase + '/abc/ExportToCSV').success(function(data, status, headers, config) {
       temp = data;
    })
   return temp;
};
 //here data is string separated by comma and new line for csv format.

aspx file:
<Button ID="ExportToCSV" ng-csv="getArray" filename="test.csv" lazy-load="true"
>ExportToCSV</Button>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

jrs*_*ala 8

temp变量分配给异步的,这意味着return temp;实际上返回undefined.

ng-csv属性接受promises,因此您应该使用$q:

$scope.getArray = function () {
    var deferred = $q.defer();

    $http
        .get(Config.serviceUrlBase + '/abc/ExportToCSV')
        .success(function(data, status, headers, config) {
           deferred.resolve(data);
        });

    return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)

注意:$q您可以在控制器中注入服务,也可以注入任何可以注入内容的服务.


Mar*_*ari 5

你错过了'()'.

ng-csv =" getArray() "


Moh*_*out 5

我有同样的问题,它由以下解决:

$scope.getArray = function () {

   var temp = $http.get(Config.serviceUrlBase + '/abc/ExportToCSV').success(function(data, status, headers, config) {
      return data;
   })
   return temp;
};
Run Code Online (Sandbox Code Playgroud)

问题是因为默认的临时变量是undefined.所以,你应该等到变量从promise返回值.