Angular Pipe(过滤器)使用笔记

pipe-test.html

<div class="pipe_div">
  <div class="pipe_title">日期:{{today}}</div>
  <div>
    <label>today | date</label>
    <strong>{{today | date}}</strong>
  </div>

  <div>
    <label>today | date: 'y-MM-dd' </label>
    <strong>{{today | date: 'y-MM-dd'}}</strong>
  </div>

  <div>
    <label>today | date: 'y-MM-dd HH:mm:ss' </label>
    <strong>{{today | date: 'y-MM-dd HH:mm:ss'}}</strong>
  </div>

  <div>
    <label>tool.formatDate(this.today)</label>
    <strong>{{todayStr}}</strong>
  </div>
</div>

<div class="pipe_div">
  <div class="pipe_title">数字:{{money}}</div>
  <div>
    <label>money | number: '3.4-5'</label>
    <strong>{{money | number: '3.4-5'}}</strong>
  </div>

  <div>
    <label>money | number: '2.2-2'</label>
    <strong>{{money | number: '2.2-2'}}</strong>
  </div>

  <div>
    <label>money | currency: 'USD': false </label>
    <strong>{{money | currency: 'USD': false }}</strong>
  </div>

  <div>
    <label>money | currency: 'CNY': true: '3.1-3'</label>
    <strong>{{money | currency: 'CNY': true: '3.1-3'}}</strong>
  </div>

  <div>
    <label>money | percent</label>
    <strong>{{money | percent}}</strong>
  </div>

  <div>
    <label>money | percent: '3.1-3'</label>
    <strong>{{money | percent: '3.1-3'}}</strong>
  </div>
</div>

<div class="pipe_div">
  <div class="pipe_title">字符串:{{abc}}</div>
  <div>
    <label>abc | slice: 2 : 4</label>
    <strong>{{abc | slice: 2 : 4}}</strong>
  </div>
  <div>
    <label>abc | lowercase | uppercase </label>
    <strong>{{abc | lowercase | uppercase }}</strong>
  </div>
</div>

pipe-test.ts

import { Component, OnInit } from '@angular/core';
import { ToolService } from '../service/tool.service';

@Component({
  selector: 'app-pipe-test',
  templateUrl: './pipe-test.component.html',
  styleUrls: ['./pipe-test.component.scss']
})
export class PipeTestComponent implements OnInit {
  today: Date;
  money: number;
  abc: string;
  todayStr: String;
  constructor(private tool: ToolService) { }

  ngOnInit() {
    this.today = new Date();
    this.money = 9999999999.999;
    this.abc = 'abcdeFGhabc';
    this.todayStr = this.tool.formatDate(this.today);
  }

}

pipe-test.scss

.pipe_div {
    padding: 8px 0;
    div label {
        display: inline-block;
        width: 200px;
    }
    div strong {
        width: 200px;
    }
    .pipe_title {
        font-size: 18px;
        padding: 6px;
        font-weight: 700;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值