0

I created this custom pipe:

@Pipe({ name: 'orderBy' })
export class OrderByPipe implements PipeTransform {
     transform(items: any[], orderBy: string): any[] {
        if (items && items.length > 1) {
            console.log('items -> ', items);
        }
        console.log('return -> items -> ', items);
        return items;
     }
 }

I am using it in a component:

<tr *ngFor="let item of items | orderBy:'title'" class="myclass">

The table rows display correctly, but in the pipe the items array length is always zero. The console statement for the return from the pipe shows an array populated with objects. Why does the array appear to be empty?

3
  • 1
    Is items in your component getting data synchronously or asynchronously? It the latter, add 'async' pipe. Commented May 23, 2017 at 19:28
  • @wannadream no it isnt an observable so async pipe would not be a good idea. Commented May 25, 2017 at 13:25
  • Can you show component code? How you initialize items. Commented May 25, 2017 at 17:30

1 Answer 1

1

Just add parenthesis as shown below. This sends the item to the pipe and the result back to the ngFor loop.

<tr *ngFor="let item of ( items | orderBy:'title')" class="myclass">

See: Angular Offiical Documentation Search for "ngfor" to see examples.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.