被调用方法中的TypeScript数组修改未反映在调用函数中

时间:2019-02-12 19:09:53

标签: angular typescript

我有下面的角度6调用组件代码:

this.appDowntimeService.getAllApplications(this.message, this.appDetails);

这是调用的服务方法:

  async getAllApplications(message: any[], appDetails: any[]) {
    this.http.get(environment.serverTMODowntime + '/tmodowntime.v1/downtime/apps/all', {headers: this.headers})
      .subscribe((res: any[]) => {
        message.length = 0;
        appDetails.length = 0;
        res.forEach(x => {
          message.push(x);
          x.details.forEach(y => {
            const appDetail = <ApplicationDetail>{};
            appDetail.createdDate = y.createdDate;
            appDetail.createdBy = y.createdBy;
            appDetail.updatedDate = y.updatedDate;
            appDetail.updatedBy = y.updatedBy;
            appDetails.push(appDetail);
          });
        });
        appDetails.sort((leftSide, rightSide) => {
          if (leftSide.startTime < rightSide.startTime) return -1;
          if (leftSide.startTime > rightSide.startTime) return 1;
          return 0;
        });
        this.updated.emit('success');
      });
  }

我希望对这两个数组(消息和appDetails)的更改在调用组件代码中可见。但是,仅显示对消息数组所做的更改(在服务函数调用内部)。对appDetails数组的更改未显示在调用组件代码中。

我无法说出为什么它对一个数组起作用,而对第二个数组起作用。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先:检查x.details中是否有项目。

第二个:appDetail.startTime从未分配值。因此,在sort函数中,比较null与null。这是一个错误。为appDetail.startTime分配一个值!