iteration a json object on Ngfor in angular 2

旧时模样 提交于 2019-11-26 23:01:36
Thierry Templier

Your people object isn't an array so you can iterate over it out of the box.

There is two options:

  • You want to iterate over a sub property. For example:

    <ul>
      <li *ngFor="#person of people?.actionList?.list">
        {{
          person.label
        }}
      </li>
    </ul>
    
  • You want to iterate over the keys of your object. In this case, you need to implement a custom pipe:

    @Pipe({name: 'keys'})
    export class KeysPipe implements PipeTransform {
      transform(value, args:string[]) : any {
        if (!value) {
          return value;
        } 
    
        let keys = [];
        for (let key in value) {
          keys.push({key: key, value: value[key]});
        } 
        return keys;
      } 
    } 
    

    and use it this way:

    <ul>
      <li *ngFor="#person of people | keys">
        {{
          person.value.xx
        }}
      </li>
    </ul>
    

    See this answer for more details:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!