Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-csv with data from api calls

Tags:

angularjs

I need to assign data to ng-csv from api call. I tried this. but didn't worked

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>

Am I missing anything?

like image 319
Namita Roy Avatar asked Sep 10 '15 19:09

Namita Roy


People also ask

How to add multiple API calls to a ngrx observable?

Using the switchMap and forkJoin operators to add multiple API calls Now, we have a copy of the NgRx data store and the GetUserTypeComplete action that will be combined into one observable. We won’t need the data from the action but we will need the data from the store for multiple API calls.

How to trigger an API call from a component in angular?

Finally, we can wrap the API call in the ngOnInit () method, a lifecycle hook in Angular which is fired whenever a component finishes loading. After adding the API call in the hook, the ngOnInit hook will trigger the API call whenever the component loads.

How do I make API calls to Google’s cloud natural language API?

To make an API call to Google’s Cloud Natural Language API, you must include an API key as a query parameter. For example, let’s say you want to find named entities (ie. proper names and common nouns) in a body of text. Then you’d make the following API request, replacing API_KEY with your actual API key:

How do I get the country data in Angular Material?

Install Angular Material. Add a JSON file under the assets folder as ‘assets/state.json’. Add the following to your JSON file. Get the full JSON file from the GitHub example. Next create a service for getting the country data.


1 Answers

The temp variable is assigned to asynchronously, which means that return temp; actually returns undefined.

The ng-csv attribute accepts promises, so you should use $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;
};

Note: $q is a service that you can inject in your controller or wherever it is you can inject things.

like image 79
jrsala Avatar answered Sep 28 '22 05:09

jrsala