我的所有搜索都找到了离子1上的帖子。在以下代码中,我可以在this.corpguys
中使用ng*For
并保存到本地存储。我想进一步获取我的应用程序并将其保存到sqlite表corpguys
。我不确定是否必须JSON.stringify(data)
然后循环以将此数据插入到db中。我只是想知道是否有人用离子2看过这个例子
import {Page, Storage, SqlStorage} from 'ionic/ionic';
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';
@Page ({
templateUrl: 'build/pages/remote/remote.html',
})
export class Remote {
constructor(http: Http) {
this.http = http;
//this.corpguys = null;
this.http.get('http://test.json')
.map(res => res.json())
.subscribe
(data => {
this.corpguys = data;
//Loop through corpguys and asign varible names
this.DataStorage = new Storage(SqlStorage);
this.DataStorage.query("INSERT into corpguys (name, position, phone, cell, sms, email, other) VALUES ('namevalue', 'positionvalue', 'phonevalue', 'cellvalue', 'smsvalue', 'emailvalue', 'othervalue')");
},
err => {
console.log("screwed up again!")
}
});
}
答案 0 :(得分:7)
我决定将其保存为键值对。这是我的最终代码
import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout';
import { NavController, AlertController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
mainContact: any;
constructor(public navCtrl: NavController, public http: Http, public storage: Storage, public alertCtrl: AlertController) {
this.mainContact = null;
this.http.get('http://www.somejson.php')
.timeout(4000)
.map(res => res.json())
.subscribe(data => {
this.mainContact = data;
//console.log(data);
this.storage.set ("mainContact", JSON.stringify(data));
},
err => {
//console.log("http fail!");
this.connectionAlert();
this.storage.get("mainContact").then((value) => {
this.mainContact = JSON.parse(value));
}
);
}//end constructor
connectionAlert() {
let alert = this.alertCtrl.create({
title: 'Connection Error',
subTitle: 'Your network coverage is too weak for updates. Previously downloaded data will be used.',
buttons: ['CONTINUE']
});
alert.present();
}
}