将数组数据追加到JSON文件。使用ngCordova文件插件的AngularJs
我是angularJs的新手,我使用的是Ionic框架 我有一个现有的JSON文件。就是这样将数组数据追加到JSON文件。使用ngCordova文件插件的AngularJs,json,angularjs,angularjs-directive,ionic-framework,ionic,Json,Angularjs,Angularjs Directive,Ionic Framework,Ionic,我是angularJs的新手,我使用的是Ionic框架 我有一个现有的JSON文件。就是这样 [{"name":"sample"}] [{"name":"sample"},{"name":"sample2"}] 当我用新数据附加文件时,它就是这样附加的 [{"name":"sample"},[{"name":"sample2"}]] 但我希望我的文件是这样的 [{"name":"sample"}] [{"name":"sample"},{"name":"sample2"}] 这是我的密
[{"name":"sample"}]
[{"name":"sample"},{"name":"sample2"}]
当我用新数据附加文件时,它就是这样附加的
[{"name":"sample"},[{"name":"sample2"}]]
但我希望我的文件是这样的
[{"name":"sample"}]
[{"name":"sample"},{"name":"sample2"}]
这是我的密码:
var savearray =[];
$cordovaFile.readAsText('user.json')
.then(function (success) {
$scope.quotes = JSON.parse(success);
savearray.unshift(JSON.parse(success,true));
console.log("Successful in reading the file initially:"+JSON.stringify(savearray));
}, function (error) {
console.log("error reading the file"+JSON.stringify(error))
});
这就是我将数据写入文件的方式
$scope.saveQuote = function(data){
savearray.unshift(data);
$cordovaFile.writeFile('user.json',JSON.stringify(savearray),{append:false})
.then(function (success) {
console.log("success the file has been created")
}, function (error) {
console.log("error"+JSON.stringify(error))
});
}
如果有人能帮助我,我会很高兴的。谢谢。什么是数据?如果它是一个数组,那么它会解释你看到了什么。我应该补充一点,如果它是一个数组,试着浓缩-。它是一个数组。顺便说一下。当我读取数据时,我使用了unshift。因此数组进入了savearray变量中。我刚刚使用了=运算符。