在 Angular 中,可以使用管道来转换和格式化数据。对于文本转换,我们可以创建自定义管道。下面是创建自定义管道来更改文本的步骤:
以下是示例代码:
custom.pipe.ts
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'custom'
})
@Injectable()
export class CustomPipe implements PipeTransform {
transform(value: string): string {
return "Custom " + value;
}
}
component.html
{{ 'Text' | custom }}
输出: Custom Text