Avatar ·

如何在Angular中为*ngFor指令应用过滤器?

大家好!我是Angular和TypeScript的新手,在模板中过滤数据时遇到了问题。我有一个组件,通过*ngFor显示用户列表。我需要按名称过滤这个列表(例如,只显示名字以特定字母开头或包含子字符串的用户)。我尝试使用pipe,但出了点问题。 这是我的代码: ```typescript // user.model.ts export interface User { id: number; name: string; email: string; } ``` ```typescript // users.component.ts import { Component } from '@angular/core'; import { User } from './user.model'; @Component({ selector: 'app-users', templateUrl: './users.component.html' }) export class UsersComponent { users: User[] = [ { id: 1, name: '安娜', email: 'anna@example.com' }, { id: 2, name: '鲍里斯', email: 'boris@example.com' }, { id: 3, name: '维克多', email: 'viktor@example.com' } ]; } ``` ```html

登录 以留下回答

博客