I have a form in an AngularJS application that, when submitted, asynchronously grabs json from a server and displays it on the screen.
I'd like to add a second button to the form, so that when it is clicked, the form takes the json from the server, converts it to csv format, and downloads it to the computer who click that button.
My current controller code looks like this:
angular.module('myApp')
.controller('QueryCtrl', ['$scope', 'Query', function ($scope, Query) {
$scope.onSubmit = function() {
var json = $.post('http://localhost:3000/api/query', { 'input': $scope.query_box });
json.done(function(result){
$scope.queries = result;
$scope.$apply();
})
};
}
])
I'd like to add another event handler function that is almost exactly the same, but instead of changing the value of $scope.queries, it will instead put the result data into a csv file for download. Something like (warning: pseudocode):
$scope.csvSubmit = function() {
var json = $.post('http://localhost:3000/api/query', { 'input': $scope.query_box });
json.done(function(result){
my_csv = createCSV();
download(my_csv);
})
};
How can I accomplish this?