转换角js1.X过滤器到angular js 2.0管道

convert angular js1.x filter to angular js 2.0 pipes

本文关键字:js 管道 angular js1 过滤器 转换      更新时间:2023-09-26

我需要转换角1。X过滤器到angular 2.0管道请帮我一下,下面是我的angular js 1。X筛选码

$scope.selectname1={};    
    $scope.selectname2={};    
    $scope.selectname3={};
    $scope.filter1 = function(item){
      return (!($scope.selectname1&&$scope.selectname1.id)||item.id !=$scope.selectname1.id);
    };
    $scope.filter2 = function(item){
      return (!($scope.selectname2&&$scope.selectname2.id)||item.id!=$scope.selectname2.id);
    };
    $scope.filter3 = function(item){
      return (!($scope.selectname3&&$scope.selectname3.id)||item.id !=$scope.selectname3.id);
    };

创建管道类

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'filter1'})
export class ExponentialStrengthPipe implements PipeTransform {
  transform(value: number, exponent: string): number {
    return exponent; // do something with your vallue
  }
}

之后在你的组件中添加并包含pipe

import { Component } from '@angular/core';
import { ExponentialStrengthPipe } from './exponential-strength.pipe';
    @Component({
      selector: 'power-booster',
      template: `
        <h2>Power Booster</h2>
        <p>Super power boost: {{2 | filter1}}</p>
      `,
      pipes: [ExponentialStrengthPipe]
    })
    export class PowerBoosterComponent { }