管道不工作

Pipe is not working

我的管道文件看起来像这样:

pipe.ts

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

@Pipe({   name: 'unique',   pure: false 
}) export class UniquePipe implements PipeTransform {

  transform(value: any, args?: any): any {
        // Remove the duplicate elements
        let uniqueArray = value.filter(function (el, index, array) { 
          return array.indexOf (el) == index;
        });

        return uniqueArray;

       }

}

组件 ts

import { UniquePipe } from './../../../services/unique.pipe';

组件html

   <li *ngFor="let dealOPtion of
deal['products'][dealindex]['options'];let dealOpt = index |
unique">
                        {{ dealOpt }}
                        {{dealOPtion['option_name'] |json}}
     </li>

我觉得应该是这样的

  <li *ngFor="let dealOPtion of
(deal['products'][dealindex]['options']  |
unique) ;let dealOpt = index">

收集后放入piple


根据评论,您忘记在模块级别声明您的管道,因此请执行以下操作

@NgModule({
  imports: [
  ],
  declarations: [UniquePipe ],
  providers: [],
  exports: [UniquePipe ]//this is needed if you want to export pipe
})
export class PipesModule { }