数组推送无法正常工作

时间:2017-09-13 22:46:02

标签: arrays angular service

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

1 个答案:

答案 0 :(得分:0)

您正在使用<management-interfaces> <native-interface security-realm="ManagementRealm"> <socket-binding native="management-native"/> </native-interface> : <management-interfaces> 订阅异步事件。因此,当您致电this.itemService.getMockItemState(sensor).subscribe时,事件未被触发,这是可能的。

尝试推迟this.checkValues()填充秒数或在订阅功能中移动调用:

this.checkValues()