仅为特定类型的数据应用管道或过滤器-Angular 5

问题描述 投票:0回答:1
<span class="date_value">{{SelectedDateText }}</span>
......
....
  <input matInput [matDatepicker]="picker"  [(ngModel)]="SelectedDateText"  >
......

“SelectedDateText”的数据类型是字符串,在某些情况下它也具有字符串值,如“今天”,“昨天”。但是当从matDatepicker中选择日期时,它显示数据(2018年2月27日00:00:00 GMT + 0500(巴基斯坦标准时间))。我不能在SelectedDateText上应用过滤器,因为它也包含字符串。如果value是date数据类型或其他类似的东西,我怎么能设法应用过滤器

angular angular-material formatdatetime
1个回答
2
投票

有点像(有礼貌的Angular2 use basic pipe in custom pipe

评论后编辑

import {Pipe, PipeTransform} from '@angular/core';
import {DatePipe} from "@angular/common";

@Pipe({name: 'myDatePipe'})
export class MyDatePipe extends DatePipe
{
        transform(value: any, format?: string, timezone?: string, locale?: string): string {
        if (typeof value === "string") {
            return value; // return raw string
        } else {
             return super.transform(arguments); // use the inherited datePipe transform method
        }
    }
}

应该做的伎俩

© www.soinside.com 2019 - 2024. All rights reserved.