angular自定义管道

对自定义管道的认识

管道的定义中体现了几个关键点:
  1、管道是一个带有“管道元数据(pipe metadata)”装饰器的类。
  2、这个管道类实现了PipeTransform接口的transform方法,该方法接受一个输入值和一些可选参数,并返回转换后的值。
  3、当每个输入值被传给transform方法时,还会带上另一个参数,比如我们这个管道中的exponent(放大指数)。
  4、我们通过@Pipe装饰器告诉Angular:这是一个管道。该装饰器是从Angular的core库中引入的。
  5、这个@Pipe装饰器允许我们定义管道的名字,这个名字会被用在模板表达式中。它必须是一个有效的JavaScript标识符。 比如,我们下面这个管道的名字是exponentialStrength。

PipeTransform接口

  transform方法是管道的基本要素。 PipeTransform接口中定义了它,并用它指导各种工具和编译器。 理论上说,它是可选的。Angular不会管它,而是直接查找并执行transform方法。

自定义管道需要注意

  我们使用自定义管道的方式和内置管道完全相同。
  我们必须在AppModule的declarations数组中包含这个管道。
  我们必须手动注册自定义管道。如果忘了,Angular就会报告一个错误。
  还需要注意的是,我们使用的时候的管道的名字是自定义管道用@Pipe注解的时候命名的名字。

自定义管道实例

  以下是我根据自定义管道的知识写的几个实例,有的是参考网上的实例在本地实现可行的代码,也在此提供参考

过滤todo

/*
    管道中纯管道和非纯管道之间的区别关键在于:
        如果是纯管道,他检测的深读很低,比如检测一个对象数组,对象数组中的对象的某个属性发生变化的时候,纯管道是检测不到的,这时候就需要用到非纯管道
*/
import {Pipe, PipeTransform} from ‘@angular/core‘;
/*
 * Example:
 *   todoList | todosStatusPipe:‘done‘:selectFilter
 *   其实这里我们已知一定是根据todo的done属性来过滤,那么实际上是可以将‘done‘这个传值给去了,直接在管道方法中用done来判断,但是
 *   这里主要是为了说明.引出的属性和[]引出的属性是有区别的,[]是可以传入变量来引出属性的
*/
@Pipe({
  name: ‘todosStatusPipe‘,
  pure: false
})
export class TodosStatusPipe implements PipeTransform {
  transform(value: Array<any>, filterString: string, status: string): Array<any> {
    let filterTodoList = [];
    switch(status){
        case ‘all‘:
            filterTodoList = value;
            break;
        case ‘active‘:
            filterTodoList = value.filter(todo => !todo[filterString]);
            break;
        case ‘completed‘:
            filterTodoList = value.filter(todo => todo[filterString]);
            break;
    }
    return filterTodoList;
  }
}

指数倍增管道

/*
    exponential-strength.pipe.ts
    步骤 :
        1、导入Pipe,PipeTransform
        2、通过注解定义名字,定义是纯管道还是非纯管道,默认是纯管道
        3、定义管道并继承PipeTransform
        4、实现继承的方法transform
*/
import { Pipe, PipeTransform } from ‘@angular/core‘;
/*
 * Raise the value exponentially
 * Takes an exponent argument that defaults to 1.
 * Usage:
 *   value | exponentialStrength:exponent
 * Example:
 *   {{ 2 | exponentialStrength:10 }}
 *   formats to: 1024
*/
@Pipe({name: ‘exponentialStrength‘})
export class ExponentialStrengthPipe implements PipeTransform {
  transform(value: number, exponent: string): number {
    let exp = parseFloat(exponent);
    return Math.pow(value, isNaN(exp) ? 1 : exp);
  }
}

判断性别管道

import {Pipe, PipeTransform} from ‘@angular/core‘;
@Pipe({
  name: ‘sexReform‘,
  //非纯管道
  //重点在于实现PipeTransform接口的transform方法,定义为非纯管道仅用于演示,非纯管道对性能影响较大,尽量避免。
  pure:false
})
export class SexReformPipe implements PipeTransform {
  transform(value: any, args?: any): any {
    let chineseSex;
    switch (value) {
      case ‘male‘:
        chineseSex = ‘男‘;
        break;
      case ‘female‘:
        chineseSex = ‘女‘;
        break;
      default:
        chineseSex = ‘未知性别‘;
        break;
    }
    return chineseSex;
  }
}

原文地址

https://www.jianshu.com/p/5140a91959ca

原文地址:https://www.cnblogs.com/shcrk/p/9194772.html

时间: 2024-08-04 18:16:36

angular自定义管道的相关文章

Angular自定义指令(directive)

angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content=&quo

angular自定义filter

angular自定义filter angular除了几个自带的常用的filter,还可以自定义filter,以满足不同的需求,简单研究了一下自定义filter,记录一下. 有如下场景,后台返回的数据中,status可能是英文字符串,如果在html中使用if进行挨个判断,则显得有些啰嗦,这样我们就可以使用自定义的filter实现 javasc代码: var myapp = angular.module('demoApp', []); myapp.controller('filterControll

angular 自定义指令详解 Directive

在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足你的各种需求的指令. 本篇文章的参考来自  AngularJS权威指南 , 文章中主要介绍指令定义的选项配置 废话不多说,下面就直接上代码 //angular指令的定义,myDirective ,使用驼峰命名法 angular.module('myApp', []) .directive('myDi

关于angular 自定义directive

关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp",[]).directive("expander",function(){ return{ //directive的一些属性(键值对形式)如下: /* restrict:'EA', replace:true, transclude:true, scope:{...}, templ

27自定义管道

①创建一个管道类文件   test.pipe.ts 指定transform方法对于数据和参数的处理,将结果返回 ②声明 app.module.ts import {TestPipe} from   ' *** ' @NgModule ( { declarations : [ TestPipe ] } ) ③调用自定义管道类 用法和内置管道没有区别 原文地址:https://www.cnblogs.com/shanlu0000/p/12229621.html

angular之自定义管道

1,装了angular2 的 cli之后,cmd中命令建立个管道文件 ng g p <name>; 如建一个在pipe文件中建一个add.pipe.ts文件 可以这么么写 ng g p pipe/add; 2,  add.pipe.ts内容如下: //原始内容import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'add' }) export class AddPipe implements PipeTransfo

angular自定义指令命名的那个坑

Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代表自己的命名空间.这里我们先使用my作为前缀: var myApp = angular.module('myApp', []) .directive('myDirective', function() { return { restrict: 'A', replace: true, template

angular自定义指令-directive

Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. var myDirective = angular.module('directives', []); myDirective.directive('directiveName', function($inject) { return { template: '<div></div>',

angular自定义指令基础

你可以向模块注册一个指令,像这样: <!-- lang: js --> myapp = angular.module("myapp", []); myapp.directive('div', function() { var directive = {}; directive.restrict = 'E'; /* restrict this directive to elements */ directive.template = "My first direct