这个问题应该很简单:我想将每个查询的状态推送到一个名为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