填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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']]);
}