• Angular自定义管道


    1.alarm-piple.piple.ts

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'alarmPipe'
    })
    export class AlarmPipePipe implements PipeTransform {
        transform(value: any, args?: any): any {
            let res: string;
            if (value === 1) {
                res = 'TRUE';
            } else if (value === 0) {
                res = 'FALSE';
            } else if (value === -1) {
                res = 'UNVERIFIED';
            } else {
                res = 'OPEN';
            }
            return res;
        }
    }
    
    @Pipe({
        name: 'eqtAlarm'
    })
    export class EqtAlarmPipe implements PipeTransform {
        transform(value: string): string {
            let res: string;
            if (value === 'CF') {
                res = 'Constant Fault(Hardware Failure)';
            } else if (value === 'IF') {
                res = 'Intermittent Fault';
            } else {
                res = value;
            }
            return res;
        }
    }
    
    
    

    2.piple.module.ts

    import { NgModule } from '@angular/core';
    import { AlarmPipePipe, EqtAlarmPipe} from 'app/alarm-management/alarm-pipe.pipe';
    const PIPES = [AlarmPipePipe, EqtAlarmPipe];
    
    @NgModule({
        declarations: PIPES,
        exports: PIPES
    })
    export class AlarmPipesModule {}
    
    

    3.在需要使用的组件对应的module中引入

    // example.module.ts
    
    import { NgModule } from '@angular/core';
    import { NgZorroAntdModule } from 'ng-zorro-antd';
    import { MaterialModule } from 'app/shared/material/material.module';
    import { FormsModule } from '@angular/forms';
    import { SharedModule } from 'app/shared';
    import { TranslateModule } from '@ngx-translate/core';
    import { AlarmPipesModule } from 'app/alarm-management/alarm-pipes.module';
    import { EquipmentAlarmDetailsComponent } from 'app/alarm-management/equipment-alarm-details/equipment-alarm-details.component';
    
    const COMPONENTS = [EquipmentAlarmDetailsComponent];
    @NgModule({
        imports: [NgZorroAntdModule, MaterialModule, FormsModule, SharedModule, TranslateModule, AlarmPipesModule],
        declarations: COMPONENTS,
        exports: COMPONENTS
    })
    export class EqtAlarmDetailsModule {}
    
    

    4.模板中使用

     <span>{{detailItem?.alarmType | eqtAlarm}}</span>
     <span>{{detailItem?.isGenuine | alarmPipe | titlecase}}</span>
    
  • 相关阅读:
    写个简单的搜索引擎
    C++中的三种继承关系
    《深度探索C++对象模型》调用虚函数
    一次数据库优化的对话
    读后感:你的灯亮着吗
    Linux Shell 截取字符串
    一次关于知识储备的思考
    哈夫曼树与哈夫曼编码
    二叉查找树
    jar中没有注清单属性
  • 原文地址:https://www.cnblogs.com/yuanchao-blog/p/13524554.html
Copyright © 2020-2023  润新知