3

我需要将 angular 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);
    };
4

1 回答 1

1

创建管道类

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
  }
}

之后在您的组件中添加并包含管道

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 { }
于 2016-06-30T12:58:45.757 回答