SqlStorage Ionic 2作为服务/提供程序
SqlStorage Ionic 2 as a service/provider
我有一个离子二的提供者,我有这个:
getCategory() {
this.storage.query('SELECT * FROM category')
.then((data) => {
var category = [];
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
category.push({
name: data.res.rows.item(i).name,
type: data.res.rows.item(i).type,
note: data.res.rows.item(i).note
});
}
}
// console.log(JSON.stringify(category));
return category; // is this correct?
}, (error) => {
console.log('Error -> ' + JSON.stringify(error.err));
});
}
然后,在注入服务后,我希望在我的页面上做这样的事情:
constructor(nav, theservice) {
this.nav = nav;
this.service = theservice
this.category = service.getCategory()
}
如何返回结果以便能够使用?当我控制台日志this.category
时,尝试以上操作不会返回任何结果
关于如何在ionic2中使用sqlite的教程很有帮助,但不知道如何将它们转换为服务/提供者。
更新(2016年6月15日)
我在下面的原始答案中分享了一个链接。更多的讨论发生在线程上,早期的回答方法虽然有效,但可能不是最佳实践。此处更新:
service.js
// using beta 7 ionic 2
import {Injectable} from '@angular/core';
import { Storage, SqlStorage } from 'ionic-angular';
@Injectable()
export class CategoryService {
static get parameters(){
return []
}
constructor() {
this.storage = new Storage(SqlStorage);
this.storage.query('CREATE TABLE IF NOT EXISTS category (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT)');
}
getCategory() {
return this.storage.query('SELECT id, name, type FROM category');
}
}
然后上面的服务是这样使用的:
somewhereInCategoryPage.js
文件
loadCategory() {
this.platform.ready().then(() => {
this.service.getCategory()
.then(data => {
this.category = [];
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
let item = data.res.rows.item(i);
this.category.push({
'id': item.id,
'name': item.name,
'type': item.type
});
}
}
console.log(this.category);
}, error => {
console.log('Error', error.err)
})
});
}
只是想更新一下,可能对谁知道有用。
旧答案离开此处以供参考
我终于决定了什么。在这里发帖,可能会对某人有所帮助。离子论坛上这条线索的指导
在服务/提供商中
getCategory() {
var storage = new Storage(SqlStorage);
return new Promise(function(resolve, reject) {
return storage.query('SELECT name, type, note FROM category')
.then((data) => {
// kinda lazy workaround
resolve(data.res.rows);
});
});
}
在构造函数中:
static get parameters() {
return [ [Myservice] ];
}
constructor(myservice) {
myservice.getCategory()
.then((category) => {
// recreate new array from old category array, or else:
// EXCEPTION: Cannot find a differ supporting object
// https://github.com/angular/angular/issues/6392#issuecomment-171428006
this.categories = Array.from(category);
console.log(this.categories);
})
.catch((error) => {
console.log(error);
});
}
在模板中:
<ion-list>
<button ion-item *ngFor="#category of categories">
{{ category.name }}
<br>
<ion-icon name="arrow-forward" item-right></ion-icon>
</button>
</ion-list>
相关文章:
- Redux应用程序结构-在哪里放置服务/业务逻辑
- Sails.js:我可以将一些服务/模型打包为npm并在Sails.jss应用程序中加载npm吗
- SqlStorage Ionic 2作为服务/提供程序
- 使用connect vhost为多个express.js应用程序提供服务
- 通过共享服务在两个不同ng应用程序中的控制器之间共享数据
- 如何创建可从多个应用程序使用的PHP登录web服务
- 谷歌应用程序脚本html服务和加载谷歌地图javascript api V3
- 如何使应用程序加载独立于web服务调用
- angular应用程序中的浏览器功能检测:使用服务、指令、控制器
- Rails 4 应用程序 - Heroku / Cloudfront - 资产无法从源正确服务
- 在出厂时注入服务错误: [$injector:unpr] 未知提供程序: $scopeProvider <- $scop
- Angular2文档-层次依赖注入程序(恢复服务)
- fetch事件处理程序中的fetch()未触发服务工作程序中的fetch事件
- 我可以在 angle js asp.net mvc 中调用控制器中的路由提供程序服务吗?
- Angular 提供程序服务需要 init 函数运行一次
- 如何从Azure Javascript应用程序服务中的脚本访问表
- 是否有一个应用程序/服务允许您为AJAX请求创建远程数据库
- 在Azure node.js应用程序服务中使用LevelDB时出错
- 我在哪里可以找到一个完整的列表有效的方法为给定的谷歌应用程序服务类
- 向jasmine test注入实用程序服务