4

I have a variable with huge data some thing like this.

$scope.employees = [
    {"firstName":"John", "lastName":"Doe",
      "city": "Bangalore","State":karnataka,},

    {"firstName":"Anna", "lastName":"Smith",
       "city": "Hyderabad","State":AndraPradesh,},

    {"firstName":"Peter", "lastName": "Jones",
      "city": "Mumbai","State":Maharastra,},
];

I want to save it to JSON file, when I click a button or something.

2 Answers 2

4

$scope.employees is the object to be saved to a file

$scope.savedJSON = angular.toJson($scope.employees,true);
var blob = new Blob([$scope.savedJSON],{
   type: "application/json;charset=utf-8;"
});
var downloadLink = document.createElement('a');
downloadLink.setAttribute('download', 'sampleJSON.txt');
downloadLink.setAttribute('href', window.URL.createObjectURL(blob));
downloadLink.click();
Sign up to request clarification or add additional context in comments.

Comments

-1

angular.toJson should help you out.

var app = angular.module('myApp', []);

app.controller('MainCtrl', ['$scope', '$filter', function ($scope, $filter) {
$scope.employees = [
{"firstName":"John", "lastName":"Doe",
  "city": "Bangalore","State":karnataka,},

{"firstName":"Anna", "lastName":"Smith",
   "city": "Hyderabad","State":AndraPradesh,},

{"firstName":"Peter", "lastName": "Jones",
  "city": "Mumbai","State":Maharastra,},
];



$scope.savedJSON = '';

$scope.save = function () {
    $scope.savedJSON = angular.toJson($filter('filter')($scope.employees, $scope.searchText));
};
}]);

var blob = new Blob([scope.save],{
type: "application/json;charset=utf-8;"
});
downloadLink.attr('href', window.URL.createObjectURL(blob));

2 Comments

Uhm... Why is this being downvoted? An explanation would be nice.
I guess that they didn't understand how it works, and didn't want to take the time to try it. maybe an explanation, or a Plunk?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.