如何在angularJS中将数据写入文件 [英] How to Write data to file in angularJS
本文介绍了如何在angularJS中将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
解决方案
$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();
这篇关于如何在angularJS中将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文