0
저는 Ionic2 및 TypeScript에 익숙하며 클래스, 속성, getter 및 setter로 게임을 시도합니다. 나의 첫번째 클래스는 단지 내가 다른 클래스에서 사용하려는, 정보를 정기적으로 검색하는 방법을 webSQL 데이터베이스의 일부 datas를 초기화하고 노출 : 다른 곳에서 내 응용 프로그램에서클래스의 Ionic2 전용 속성이 클래스에 설정되어 있어도 항상 비어 있습니다.
import { Injectable } from '@angular/core';
import * as PouchDB from 'pouchdb';
@Injectable()
export class ezStockService {
private _db;
private _areas: any = [];
private _ezstockzs;
initDB(){
this._db = new PouchDB('ezstockz', {adapter: 'websql'});
var self = this;
/**
* Crée éventuellement les zones de stockage par défaut
**/
this._db.get("area_1").catch(function(error){
if(error.name === 'not_found'){
return {
_id: "area_1",
libelle:[
{
"lang": "FR-fr",
"value": "Nord"
},
{
"lang": "EN-en",
"value": "North"
}
],
"logo": "images/boussole.png"
}
}
}).then(function(gmDoc){
self._db.put(gmDoc);
});
this._db.get("area_2").catch(function(error){
if(error.name === 'not_found'){
return {
_id: "area_2",
libelle:[
{
"lang": "FR-fr",
"value": "Sud"
},
{
"lang": "EN-en",
"value": "South"
}
],
"logo": "images/boussole.png"
}
}
}).then(function(fgDoc){
self._db.put(fgDoc);
});
this._db.get("area_3").catch(function(error){
if(error.name === 'not_found'){
return {
_id: "area_3",
libelle:[
{
"lang": "FR-fr",
"value": "Est"
},
{
"lang": "EN-en",
"value": "East"
}
],
"logo": "images/boussole.png"
}
}
}).then(function(cgDoc){
self._db.put(cgDoc);
});
}
/**
* Method that retrieve all areas
**/
allAreas() {
this._db = new PouchDB('ezstockz', {adapter: 'websql'});
this._areas = [];
var self = this;
this._db.allDocs({
startkey: 'area_',
endkey: 'area_\uffff',
include_docs: true
}).then(function(areas){
var rows = areas.rows;
var zones = [];
rows.forEach((item,index) => {
var area = {
"libelle": item.doc.libelle[0].value,
"image": item.doc.logo
};
zones.push(area);
});
return zones;
}).then(function(areas){
self._areas = areas;
});
}
/**
* Method that get areas
**/
get areas(){
return this._areas;
}
}
, 내가보기에 모든 영역을 표시하고 싶습니다 그래서, 다른 클래스에서, 지역과 같이 수완 사용하려고 :
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { ezStockService } from '../../services/ez-stock.service';
@Component({
selector: 'page-stocks',
templateUrl: 'stocks.html'
})
export class Stocks {
public stocks = [];
items: any;
constructor(public navCtrl: NavController,
private platform: Platform,
private stockService: ezStockService) {
this.items = [];
var self = this;
stockService.allAreas(); // Invoke method
var areas = stockService.areas; // Invoke getter
console.log("Zones : (" + areas.length + ")");
}
}
을 console.log의 결과 것은 allAreas() 메소드는 배열의 3 개 항목을 반환하는 경우에도 항상 "0"입니다 ...
왜이 동작을 이해하지 못하겠습니까 라인
stockService.allAreas(); // Invoke method
에서 타이프 라이터의 속성 범위 ...
들으 사 당신의 도움이 ...
Thx ... 호출 된 메서드에 deffered.resolve()를 써야한다는 의미입니까? –
@ Jean-LucAubert 예 할 수 있습니다. –