0
我希望能夠在輸入時輸入列表對象。但它不斷給我一個錯誤。
我認爲它必須與管道不能正確讀取值。Angular2帶對象的管道過濾器
//our root app component
import {Component, NgModule, Pipe, PipeTransform} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {FormsModule} from '@angular/forms'
@Pipe({ name: 'filter' })
export class FilterPipe implements PipeTransform {
public transform(values: any[], filter: string): any[] {
if (!values || !values.length) return [];
if (!filter) return values;
return values.filter(v => v.indexOf(filter) >= 0);
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<input [(ngModel)]="filterString" />
<div *ngFor="let d of (data | filter: filterString)">
{{ d.id }}
</div>
</div>
`,
})
export class App {
name:string;
data = [{id: 1, leagueName: 'Recreation League' },
{id: 2, leagueName: 'Recreation League' },
{id: 3, leagueName: 'Recreation League' }
];
filterString = '';
constructor() {
this.name = 'Angular2'
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App, FilterPipe ],
bootstrap: [ App ]
})
export class AppModule {}
預先感謝您
確切的錯誤消息,你會得到極大的幫助;-) –
嘿!謝謝你幫助我。我只是發佈它的答案:) – user3083272