Language
:Ionic2イオン配列2に多次元配列を割り当てたい
Description
:アイデアは、プロバイダを使用してクラウドからデータを取得しようとしています。ホームコンポーネントでは、プロバイダのメソッドを呼び出し、その結果を変数に代入しようとします。しかし、それは動作しません。試験方法は、目で
Sample Code for provider's code
lastValues: any[][] = [[]];
constructor(public http: Http) {}
getLastValues() {
this.http.get("http://things.ubidots.com/api/v1.6/datasources/" + this.dataSourcaId + "/variables/?token=" + this.ubidotsToken)
.map(res => res.json().results).subscribe(data => {
this.lastValues = Array(Math.ceil(data.length/2));
let rowNum = 0;
for (let i = 0; i < data.length; i+=2) {
this.lastValues[rowNum] = Array(2);
if (data[i]) {
this.lastValues[rowNum][0] = data[i].name;
// console.log(this.lastValues[rowNum][0]);
}
if (data[i+1]) {
this.lastValues[rowNum][1] = data[i+1].name;
}
rowNum++;
}
}, (err) => {
console.log(err);
});
return this.lastValues;
}
Sample code for home component
export class HomePage {
sensors: any[][];
constructor(public navCtrl: NavController, public dataService: Data, public platform: Platform) {
}
ionViewDidLoad(){
this.platform.ready().then(() => {
this.sensors = this.dataService.getLastValues();
});
}
test() {
console.log(this.sensors);
console.log(this.dataService.getLastValues());
}
}
最初のコマンドは空の配列を出力し、2番目のコマンドはクラウドのデータを出力します。私が間違っていることを理解できません。あなたはhttpリクエストを作っているので、それが起こっている理由がある
おかげで...
は終了する 'ionViewDidLoad'のコールバックを待たずに、' test'を呼び出す誰ですか? –