填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

KeyValuePipe

将 Object 或 Map 转换为键值对数组。

Transforms Object or Map into an array of key value pairs.

查看"说明"...

      
      {{ input_expression | keyvalue [ : compareFn ] }}
    

Exported from

输入值

input { [key: string]: V; [key: number]: V; } | ReadonlyMap

参数

compareFn (a: KeyValue, b: KeyValue) => number
可选. 默认值是 `defaultComparator`.

说明

输出数组将通过键名排序。默认情况下,比较器将使用 Unicode 点位值。如果你的键名是复杂类型,则可以选择传入一个 compareFn。

The output array will be ordered by keys. By default the comparator will be by Unicode point value. You can optionally pass a compareFn if your keys are complex types.

Further information available in the Usage Notes...

使用说明

例子

Examples

此示例演示了 ngFor 如何使用此键值管道对 Object 或 Map 进行迭代。

This examples show how an Object or a Map can be iterated by ngFor with the use of this keyvalue pipe.

      
      @Component({
  selector: 'keyvalue-pipe',
  template: `<span>
    <p>Object</p>
    <div *ngFor="let item of object | keyvalue">
      {{item.key}}:{{item.value}}
    </div>
    <p>Map</p>
    <div *ngFor="let item of map | keyvalue">
      {{item.key}}:{{item.value}}
    </div>
  </span>`
})
export class KeyValuePipeComponent {
  object: {[key: number]: string} = {2: 'foo', 1: 'bar'};
  map = new Map([[2, 'foo'], [1, 'bar']]);
}