Tho*_*oot 1 sqlite json angularjs
我所有的搜索发现对下面的代码离子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!")
}
});
}
Run Code Online (Sandbox Code Playgroud)
我决定将其保存为键值对.这是我的最终代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6091 次 |
| 最近记录: |