角度2 ES6过滤器与TS

角度2 ES6过滤器与TS

问题描述:

我不能让ES6过滤器功能与角2(使用TypeScript)一起使用。角度2 ES6过滤器与TS

我的过滤功能如下:

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

我的打字稿类是非常简单的:

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; 
    }; 

} 

虽然,我的笔记阵列是从来没有过滤,不管我会设置什么样的属性了注意构造函数。

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

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