Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

后端 未结 18 1244
逝去的感伤
逝去的感伤 2020-11-29 05:50

I looked at similar questions, but none of them helped me. I am going to receive an object like the following:

[
  {
    \"id\": 1,
    \"name\": \"Safa\",
          


        
18条回答
  •  没有蜡笔的小新
    2020-11-29 06:14

    Store that objects into Array  and then iterate the Array
    export class AppComponent {
    
     public obj: object =null;
     public myArr=[];
    
      constructor(){
    
        this.obj = {
          jon : {username: 'Jon', genrePref: 'rock'},
          lucy : {username: 'Lucy', genrePref: 'pop'},
          mike : {username: 'Mike', genrePref: 'rock'},
          luke : {username: 'Luke', genrePref: 'house'},
          james : {username: 'James', genrePref: 'house'},
          dave : {username: 'Dave', genrePref: 'bass'},
          sarah : {username: 'Sarah', genrePref: 'country'},
          natalie : {username: 'Natalie', genrePref: 'bass'}
      }
      }
      ngOnInit(){
        this.populateCode();
      }
    
      populateCode(){
        for( let i in this.obj) {   //Pay attention to the 'in'
        console.log(this.obj[i]);
        this.myArr.push(this.obj[i]);
      }
      }
     }
    
    
    
    
    {{item.username}} {{item.genrePref}}

提交回复
热议问题