這個問題應該很簡單:我想將每個查詢的狀態推送到一個名爲currentValues的數組中。 不幸的是,currentValues陣列仍然無人居住(校驗值的輸出是0vs2。我缺少什麼嗎?陣列推送不能正常工作
@Input() public wantedValue: string[];
@Input() public querySensor: string[];
@Input() public slideId: number;
@Output() wantedStateAccievedEvent: EventEmitter<any> = new EventEmitter<any>();
public currentValues: string[] = [];
initItems() {
for (let sensor of this.querySensor) {
console.log("Sensor: " + sensor);
this.itemService.getMockItemState(sensor).subscribe(
status => { this.currentValues.push((status)); });
}
this.checkValues();
}
checkValues(): void {
console.log("Called checkValues" + this.currentValues.length + "vs " + this.wantedValue.length);
let i = 0;
let allSatisfied: boolean;
for (let value of this.currentValues) {
console.log("Wert: " + value + '\n');
if (this.wantedValue[i] !== value) {
allSatisfied = false;
}
i++;
}
if (allSatisfied === true) {
this.wantedStateAccievedEvent.emit({slideId: this.slideId, stateAccieved: true });
}
的可能的複製[2角 - 直接從Observable返回數據](https://stackoverflow.com/questions/37867020/angular-2-return-data-directly-from-an-observable) – jonrsharpe
你是否檢查過「status」的值?如果添加這個,輸出是什麼:'console.log(status');'? – mok
嗨,狀態總是有一個值 - 整個事情使用數組,在更改wantedVaule和querySensor到數組後,問題出現 – jibjapda