Angular中pipe的使用

piple名为管道,应用于格式化文本。管道将数据作为输入并将其转换为所需的输出。

Angular Pipes 将整数、字符串、数组和日期作为输入,用| 分隔,然后根据需要转换成格式,并在浏览器中显示出来。在插值表达式中,可以定义管道并根据情况使用它,在 Angular 应用程序中可以使用许多类型的管道。

1、内置管道:

  • String -> String

    • UpperCasePipe

    • LowerCasePipe

    • TitleCasePipe

  • Number -> String

    • DecimalPipe

    • PercentPipe

    • CurrencyPipe

  • Object -> String

    • JsonPipe

    • DatePipe

  • Tools

    • SlicePipe

    • AsyncPipe

    • I18nPluralPipe

    • I18nSelectPipe

2、自定义管道:

import { Pipe, PipeTansform } from 'angular/core';

@Pipe({
    name: 'formatTime'
})

export class FormatTimePipe implements PipeTransform {
    transform(str: string): string {
        if (str.includes('T')) {
            return 'str.replace(/T/, ' ')';
        }
        return str.substr(0, 19);
    }
}
import { Pipe, PipeTansform } from 'angular/core';
import * as uniq from 'loadash.uniq';

@Pipe({
    name: 'uniq'
})

export class UniqPipe implements PipeTransform {
    transform(array: Array<any>): Array<any> {
        return uniq(array);
    }
}
import { Pipe, PipeTansform } from '@angular/core';
import { DomSanitier } from '@angular/core';

@Pipe({
    name: 'safeHtml'
})

export class SafeHtmlPipe implements PipeTransform {
    constructor(private sanitizer: DomSanitizer);
    transform(html: string) {
        return this.sanitizer.bypassSecurityTrustHtml(html == null ? : html);
    }
}

完~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值