0

My data is arranged in ascending order... and i want it in descending order. I am using angular4. For this i am using a reverse pipe. The code for the same is as below. The first time i used it...it worked ..but later from second time onwards it has started giving error as --> The pipe 'reverse' could not be found.

This is were i referred the code from -

Invert Angular 2 *ngFor

transaction.component.ts

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

 @Pipe({  
  name: 'reverse',
  pure:false
 })


export class TransactionComponent implements OnInit ,PipeTransform{

transform (values: any) {
 if (values) {
  return values.slice().reverse();
   }
  }

 .....//code
 }

transaction.component.html

 <tr *ngFor="let dat of result |reverse |  filter:filterdata| 
  paginate: { itemsPerPage: 5, currentPage: p };let i = index ">
  • Did you declare your pipe in a module? – Anuradha Gunasekara May 17 '18 at 06:29
  • I just added -->import { Pipe, PipeTransform } from '@angular/core'; in my master.module.ts.... anywhere we need to declare it in imports or declarations ? –  May 17 '18 at 06:31
  • Yeah you need to add your pipe to the declarations array – Anuradha Gunasekara May 17 '18 at 06:36
  • please show you json data – Sharma Vikram May 17 '18 at 06:37
  • this is part of my json - "TESTNODE_root" BTID : "1_BUQPT2554D_20180410-172211" CORREL_ID : "000000000000000000000000000000000000000000000000" CREATED_BY : "SYSTEM" CREATED_ON : "2018-04-10T11:52:13.000Z" DESCRIPTION : null ERROR_CD : null ERROR_COMPONENT : null ERROR_TYPE : null EXCEPTION_PAYLOAD_ID : null FILE_ID : null INTERFACE_NAME : "Experian" INW_TRN_ID : null LOG_ID : 2 –  May 17 '18 at 07:02
  • i had a different approach here...https://stackoverflow.com/questions/50371770/unable-to-sort-using-pipetransform-in-angular4/50371886#50371886 ......but it is not working ... can you please check it –  May 17 '18 at 07:03

2 Answers2

1

The below code declares a pipe. reverse.pipe.ts

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

 @Pipe({  
  name: 'reverse',
  pure:false
 })


export class ReversePipe implements PipeTransform{

transform (values: any) {
 if (values) {
  return values.slice().reverse();
   }
  }

 .....//code
 }

In order to use it in your app.You need to import the pipe and add it to the declarations in your module like below.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReversePipe } from '../path/to/yourpipe'

@NgModule({
  declarations: [
    AppComponent,
    ReversePipe
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Working demo on Stackblitz

Hope this helps :)

Manish
  • 4,692
  • 3
  • 29
  • 41
  • Hey Manish, ...is it necessary to define a ReversePipe differently... because i am defining its content TransactionComponent and it is already defined in my declarations : [TransactionComponent] ... i have written the code as it is in my question. It that declaration not correct. Because you seem to define another .ts file where the reverse function code is kept –  May 17 '18 at 06:49
  • And in your stackblitz ... the hello is not printed in reverse ... sorry to notice –  May 17 '18 at 06:56
  • i had a different approach here...https://stackoverflow.com/questions/50371770/unable-to-sort-using-pipetransform-in-angular4/50371886#50371886 ......but it is not working –  May 17 '18 at 06:58
  • @SuchetaShrivastava yes the code is not to print it in reverse. Thats just a demo. Will edit it to print it in reverse. Yes it is a good practice to define it seperately. Also component and Pipe are two different things. Can read more about them in offical docs – Manish May 17 '18 at 07:30
  • Have updated the demo. Will print in reverse. Also you will have to implement proper logic to handle different value. Currently it will work for only strings – Manish May 17 '18 at 07:32
0

Add your pipe to declarations array in your module.

@NgModule({
   declarations: [
       // import and add your pipe to this.
   ],

   imports: [

   ],
   providers: [],
   bootstrap: []
})
export class AppModule { }
Anuradha Gunasekara
  • 6,553
  • 2
  • 27
  • 37
  • if i add to declarations : [Pipe] ...i get this error ... Error: Unexpected value 'DecoratorFactory' declared by the module 'MasterSystemModule'. Please add a @Pipe/@Directive/@Component annotation. .... Also, i have defined my component name in declarations :[TransactionComponent] –  May 17 '18 at 06:46
  • i had a different approach here...https://stackoverflow.com/questions/50371770/unable-to-sort-using-pipetransform-in-angular4/50371886#50371886 ......but it is not working... can you please check it –  May 17 '18 at 07:03