Angular2 Sorting Pipe with Object Array

后端 未结 3 1748
余生分开走
余生分开走 2021-01-14 05:01

How to make a sorting pipe in angular2 with an array of objects

Original Problem:

I have a TODOs list, (Todo[ ]) and I want to sort it every time I make

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 05:34

    Perhaps the value todos is null at the beginning because it's loaded asynchronously using HTTP.

    To prevent from such use case you could add this in your pipe:

    @Pipe({
      name: "sort"
    })
    export class TodosSortPipe implements PipeTransform {
      transform(array: Todo[], args: any): Todo[] {
        if (array == null) {
          return null;
        }
        (...)
      }
    }
    

    Then the value todos will be received and the transform method of the pipe will be called again with this non null value...

    Moreover it seems that your

  • tag isn't ended. You must have valid HTML into component templates. I don't know if it's the complete code or a truncated one...

    Hope it helps you, Thierry

提交回复
热议问题