禁用默认的键值管道排序

Abd*_*hid 4 angular-pipe angular

<tr *ngFor="let a of arrayOfObjects">
    <td *ngFor="let item of cfValues | keyvalue">
        {{item.value}}
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我只是想按常规顺序打印项目,但是键/值管道会根据索引进行默认排序。有没有办法禁用默认排序?

alb*_*esi 17

如果您不希望订购它们,则需要返回0。因此,您可以这样做 <td *ngFor="let item of cfValues | keyvalue : 0">

但这会引发ts错误: TypeError: The comparison function must be either a function or undefined

否则,您可以创建一个返回0的函数并使用

function returnZero() {
return 0
}

[... in your template]
<td *ngFor="let item of cfValues | keyvalue : returnZero">
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案! (4认同)
  • 如果您使用打字稿,该函数应该类似于:`returnZero() { return 0; }` (2认同)