【问题标题】:Disable the default keyvalue pipe sort in angular禁用角度中的默认键值管道排序
【发布时间】:2019-01-08 11:36:18
【问题描述】:
<tr *ngFor="let a of arrayOfObjects">
    <td *ngFor="let item of cfValues | keyvalue">
        {{item.value}}
    </td>
</tr>

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

【问题讨论】:

  • 默认排序? IMK,这取决于浏览器如何提取密钥,JS 对象是无序的。

标签: angular angular-pipe


【解决方案1】:

如果您不希望它们被订购,您需要返回 0。 所以在你的事业中你可以做 &lt;td *ngFor="let item of cfValues | keyvalue : 0"&gt;

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

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

returnZero() {
    return 0
}

[...在您的模板中]

<td *ngFor="let item of cfValues | keyvalue : returnZero">

【讨论】:

  • 如果你使用 typescript,函数应该是这样的:returnZero() { return 0; }
猜你喜欢
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2022-08-24
  • 2019-03-17
相关资源
最近更新 更多