为什么要使用角度2的“过滤器"被称为“管道"? [英] Why angular 2 "Filters" are called "Pipe"?

查看:39
本文介绍了为什么要使用角度2的“过滤器"被称为“管道"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角度1 中,当我们希望格式化表达式的值以显示给用户时,我们使用angular过滤器.在角度2 中,我们使用

In Angular 1, when we want to format the value of an expression for display to the user we use angular Filters. In Angular 2, We use Pipe for the same.

角度1过滤器:

HTML:

<p> {{ greetings | reverse }}</p>

Js:

app.filter('reverse', function() {
  return function(input, uppercase) {
    input = input || '';
    var out = '';
    for (var i = 0; i < input.length; i++) {
      out = input.charAt(i) + out;
    }
    // conditional based on optional argument
    if (uppercase) {
      out = out.toUpperCase();
    }
    return out;
  };
});

角度2 :

HTML:

<p> {{ greetings | reverse }}</p>

TypeScript:

TypeScript:

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

@Pipe({name: 'reverse'})
export class ReversePipe {
  transform(input:string): string {
    input = input || '';
    let out = '';
    for (var i = 0; i < input.length; i++) {
      out = input.charAt(i) + out;
    }
    return out;
  }
}

通过查看代码,可以得出结论,两者都做相同的事情.两者具有相同的作用和责任.我想知道的是为什么将它们重命名为Angular 2中的Pipe?

By looking at code, one can conclude that both does the same thing. Both have same roles and responsibility. What I want to know is Why they are renamed to Pipe in Angular 2?

推荐答案

因为Pipe具有更一般的含义,不仅用于过滤.

Because Pipe has a more general meaning and is used not only for filtering.

您可能还想阅读文档关于为何在Angular 2中缺少 filter

You may also want to read the paragraph from docs about why filter is absent in Angular 2:

Angular不附带用于过滤或排序列表的管道.熟悉Angular 1的开发人员将其称为filter和orderBy.Angular 2中没有等效项.

Angular does not ship with pipes for filtering or sorting lists. Developers familiar with Angular 1 know these as filter and orderBy. There are no equivalents in Angular 2.

这不是疏忽.Angular 2不太可能提供此类管道因为(a)他们的表现不佳,并且(b)他们防止侵略性缩小filter和orderBy都需要以下参数:参考对象属性.我们之前了解到,此类管道必须是不纯的,并且Angular在几乎所有更改中都称不纯管道检测周期.

This is not an oversight. Angular 2 is unlikely to offer such pipes because (a) they perform poorly and (b) they prevent aggressive minification. Both filter and orderBy require parameters that reference object properties. We learned earlier that such pipes must be impure and that Angular calls impure pipes in almost every change detection cycle.

过滤,尤其是排序是昂贵的操作.用户即使是中等大小的列表,体验也会严重降低Angular每秒多次调用这些管道方法.过滤器和Angular 1应用经常滥用orderBy,导致抱怨Angular本身很慢.该指控在间接的意义是Angular 1通过首先提供filter和orderBy.

Filtering and especially sorting are expensive operations. The user experience can degrade severely for even moderate sized lists when Angular calls these pipe methods many times per second. The filter and orderBy have often been abused in Angular 1 apps, leading to complaints that Angular itself is slow. That charge is fair in the indirect sense that Angular 1 prepared this performance trap by offering filter and orderBy in the first place.

这篇关于为什么要使用角度2的“过滤器"被称为“管道"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆