Angular 如何使用对象更新存储
嗨,我知道如何在ionic2中使用本地存储。每次设置和获取数据时,我都可以轻松获取这些对象,但现在我需要存储在其中的以前的数据Angular 如何使用对象更新存储,angular,ionic2,Angular,Ionic2,嗨,我知道如何在ionic2中使用本地存储。每次设置和获取数据时,我都可以轻松获取这些对象,但现在我需要存储在其中的以前的数据 // This form will have user name which is a string import { Storage } from '@ionic/storage'; logForm(form) { if (form.valid) { console.log(form.value); this.myData =
// This form will have user name which is a string
import { Storage } from '@ionic/storage';
logForm(form) {
if (form.valid) {
console.log(form.value);
this.myData = form.value;
this.setData();
}
}
setData(){
this.storage.set('userObj', JSON.stringify(this.myData));
this.getData();
}
getData(){
this.storage.get('userObj').then((data) => {
console.log("get data", JSON.parse(data));
});
}
当一个新的sting被添加到“userObj”中时,它应该被用户提供的一组新字符串更新。谁能帮帮我吗 试试这个
// This form will have user name which is a string
import { Storage } from '@ionic/storage';
logForm(form) {
if (form.valid) {
console.log(form.value);
let dataExist: Array<any> = [];
let storageData = this.storage.get('userObj').then((data) => {
dataExist.push(JSON.parse(data));
});
dataExist.push(form.value)
this.myData = dataExist;
this.setData();
}
}
setData(){
this.storage.set('userObj', JSON.stringify(this.myData));
this.getData();
}
getData(){
this.storage.get('userObj').then((data) => {
console.log("get data", JSON.parse(data));
});
}
//此表单的用户名为字符串
从'@ionic/Storage'导入{Storage};
日志格式(表格){
如果(表格有效){
console.log(form.value);
让数据存在:数组=[];
让storageData=this.storage.get('userObj')。然后((data)=>{
push(JSON.parse(data));
});
dataExist.push(form.value)
this.myData=dataExist;
这是setData();
}
}
setData(){
set('userObj',JSON.stringify(this.myData));
这是getData();
}
getData(){
this.storage.get('userObj')。然后((数据)=>{
log(“获取数据”,JSON.parse(data));
});
}
对不起,您的问题我不清楚?你需要用新对象替换现有对象吗?不,我需要用一个新对象更新现有对象对象对象被重写,我需要调用getData()方法@Pardeep Jain时的新对象和旧对象