Iterate over object in Angular

前端 未结 17 1590
攒了一身酷
攒了一身酷 2020-11-22 12:19

I am trying to do some things in Angular 2 Alpha 28, and am having an issue with dictionaries and NgFor.

I have an interface in TypeScript looking like this:

17条回答
  •  一向
    一向 (楼主)
    2020-11-22 12:51

    Angular 6.1.0+ Answer

    Use the built-in keyvalue-pipe like this:

    Key: {{item.key}} and Value: {{item.value}}

    or like this:

    Key: {{item.key}} and Value: {{item.value}}

    where mySortingFunction is in your .ts file, for example:

    mySortingFunction = (a, b) => {
      return a.key > b.key ? -1 : 1;
    }
    

    Stackblitz: https://stackblitz.com/edit/angular-iterate-key-value

    You won't need to register this in any module, since Angular pipes work out of the box in any template.

    It also works for Javascript-Maps.

提交回复
热议问题