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());
}
}
在試驗方法中,第第一個命令打印空數組,第二個命令打印來自雲的數據。無法理解我做錯了什麼事。
謝謝...
是否有人調用'test'無需等待'ionViewDidLoad'的回調來完成? –