过滤可观察的响应并保存到局部变量

时间:2017-10-16 07:10:42

标签: angular service filter

我有一个返回ApiResponse对象的服务,其中结果是Client对象的向量。该服务运行良好,并正确接收数据。问题是,在我想要呈现信息的组件中,我需要过滤该服务的结果以在两个部分中显示它们,但这是不可能的,因为过滤的向量总是为空的。另一方面,我不明白的是,为什么当我尝试在OnInit中显示每个控制台的矢量客户端时,它会将其返回为空,但如果它获取数据并显示它,我会在模板中使用它。

这是我的代码:

export class PartnersExpirationsComponent implements OnInit {
  expirations: Client[] = [];
  expirationsWithoutDays: Client[] = [];
  expirationsWithDays: Client[] = [];

  constructor(
    private partnersExpirationsService: PartnersExpirationsService
  ) { }

  ngOnInit() {
    this.getData();
  }

  private getData(): void {
    this.partnersExpirationsService.getClients()
      .subscribe(
      next => {
        console.log(next);
        this.expirations = next.results;
        this.expirationsWithoutDays = this.expirations.filter(client => {
          return client.extraDays === false;
        });
        this.expirationsWithDays = this.expirations.filter(client => {
          return client.extraDays === true;
        });
      },
      error => this.handleError
      );
  }
}

我正在经历的事情,我没有意识到?

编辑:这是在通过控制台打印时返回下一个模型的模型:

export interface IARClient {
    info: {
        results: number,
        version: string,
    };
    results: Client[];
}

export class Client {
    constructor(
        public name: string,
        public lastName: string,
        public numberDni: number,
        public email: string,
        public street: string,
        public phone: number,
        public alternatePhone: number,
        public birthdate: Date,
        public dateRegister: Date,
        public balance: number,
        public extraDays: boolean,
        public district: District,
        public state: State,
        public gender: Gender,
        public associationXClient: AssociationXClient,
        public id?: number,
    ) { }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

哦,大声笑,我刚刚发现你的错字:

您在extraDays上过滤,并且您返回的属性是extrasDays。

现在享受你的空闲时间:)