离子2 ng用于查看视图中的更新数据

时间:2017-01-03 12:41:24

标签: angular typescript ionic2 ngfor

在我的项目'无限滚动日历' 中,我使用ngFor使用' pipe'进行交互。 mapToIterable。

不幸的是,我注意到当我向对象添加新属性时,视图变量已更新但ng不更新列表。 问题是,当我进入视图时,它填充正常,但当我向下滚动它成功从API检索数据但不更新ngFor中的列表..

我通过在zone.run()中放置代码而没有成功来尝试解决方法问题。

#ionic info

Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 0.0.45
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.4
Node Version: v6.5.0
Xcode version: Not installed

某些代码的时间:

// mapToIterable.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'mapToIterable'})
export class MapToIterablePipe implements PipeTransform {
  transform(value): any {
    let keys = [];
    for (let key in value) {
      keys.push({ key: key, value: value[key]});
    }
    return keys;
  }
}

// index.ts
export class Appointments {
  appointments = {};
  current_date = new Date();
  top_date: any = new Date(+new Date - 12096e5);  // 14 days past
  bottom_date: any = new Date(+new Date + 12096e5);  // 14 day future
  range = {
    start_date: this.top_date,
    end_date: this.bottom_date
  }

  generateDays = function (start, end) {
    while(start < end) {
      start.setDate(start.getDate() + 1);
      this.appointments[start.toISOString().slice(0, 10)] = [];
    }
  };

  fillDays = function () {
    this.appointmentService.all(this.range).map(res => res.json()).subscribe(
      data => {
        for (var appointment in data) {
          this.appointments[appointment] = data[appointment];
        }
      },
      err => {
        console.log(JSON.parse(err._body));
      }
    );
  };

  constructor(
    public navCtrl: Nav,
    public appointmentService: AppointmentService,
  ) {
    var temp_date: any = new Date(this.top_date);
    this.generateDays(temp_date, this.bottom_date);
    this.fillDays();
  }

  moreFuture(infiniteScroll) {
    setTimeout(() => {
      var temp_date: any = new Date(this.bottom_date);
      this.bottom_date = new Date(+temp_date + 12096e5); // add 14 days
      this.range.start_date = temp_date;
      this.range.end_date = this.bottom_date;
      this.generateDays(temp_date, this.bottom_date);
      this.fillDays();
      infiniteScroll.complete();
    }, 500);
  };
}

// index.html
 <ion-content padding>
  {{appointments | json}}
  <ion-list class="calendar-list">
    <div *ngFor="let appointment of appointments | mapToIterable" [id]="appointment.key">
      <ion-item class="day past">
        <ion-avatar class="date" item-left>
          <h1>{{appointment.key | date: 'dd'}}</h1>
          <h2>{{appointment.key | date: 'MMM'}}</h2>
        </ion-avatar>
        <div *ngIf="!!appointments[appointment.key].length">
          <ion-avatar class="inline" text-center padding>
            {{appointments[appointment.key][0].patient.first_name[0]}}{{appointments[appointment.key][0].patient.last_name[0]}}
          </ion-avatar>
          <div class="inline">
            <h2 class="username" text-wrap>
              {{appointments[appointment.key][0].patient.first_name}}
              {{appointments[appointment.key][0].patient.last_name}}
            </h2>
            <p>
              <ion-icon name="clock-gray"></ion-icon>
              {{appointments[appointment.key][0].time_start | date: 'HH:mm'}}
              -
              {{appointments[appointment.key][0].time_end | date: 'HH:mm'}}
            </p>
          </div>
        </div>
      </ion-item>
    </div>
    <ion-infinite-scroll (ionInfinite)="moreFuture($event)">
      <ion-infinite-scroll-content></ion-infinite-scroll-content>
    </ion-infinite-scroll>
  </ion-list>
</ion-content>

原因是我使用的是对象而不是数组?

我将感激每一个答案:)

1 个答案:

答案 0 :(得分:3)

这是因为您使用的是“纯管道”,它在应用它的值或引用时会执行更改。 它不会检测对象属性更改。在您的情况下,mapToIterable管道不知道对象属性发生的更改Check here

尝试

@Pipe({name: 'mapToIterable',pure:false})