带有TS的Angular 2 ES6滤波器

时间:2017-06-04 19:54:18

标签: javascript angular typescript

我无法使ES6过滤功能与Angular 2(使用TypeScript)一起使用。

我的过滤功能如下:

getApplicableNotes(): void {
this.noteService
  .getNotes()
  .then(notes => {
    notes.filter((note) => !note._deleted && !note._done);
    this.notes = notes;
  })
  .catch((error) => this.error = error);
}

我的TypeScript类非常简单:

export class Note {
  id: number;
  title: string;
  description: string;
  _starred = false;
  _done = false;
  _deleted = false;

  constructor(id: number, title: string, description: string, starred?: boolean, done?: boolean, deleted?: boolean) {
    this.id = id;
    this.title = title;
    this.description = description;
    this._starred = starred ? starred : false;
    this._done = done ? done : false;
    this._deleted = deleted ? deleted : false;
  };

}

虽然我的notes数组永远不会被过滤,无论我在Note构造函数中设置什么属性。

1 个答案:

答案 0 :(得分:2)

filter()方法不会修改数组,但会返回一个新的过滤数组。您应该将结果分配给this.notes

.then(notes => {
  this.notes = notes.filter((note) => !note._deleted && !note._done);
})