ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables Angular 9

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables Angular 9

大家好,我有这个错误错误错误:找不到类型 'object' 的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables

我试过了。json 但它在 angular 9

中不起作用

我的组件.html

  <div class="form-group">
                <label for="">Ville</label>
                <select class="form-control" #hh
                    (change)="getidregion(hh.value)">
                    <option></option>
                    <option *ngFor="let ville of dropDownList"
                        [value]="ville.id">
                        {{ville.nomVille}}
                    </option>

                </select>

            </div>

我的Component.TS:

dropDownList: [];

  responsable = {

    id: 0,
    nomResponsable: '',
    prenomResponsable: '',
    emailResponsable: '',
    mdpResponsable: '',
    img: '',
    rib: '',
    dtn: '',
    region: 0,
    ville: 0,
  };

    methodAPIToGetVilles() {
        this.res.getville()
          .subscribe((s: any) => {
            this.dropDownList = s;
    
          });
      }
    
      //methode to get the value off the idville in the option
      getidville(id: any): void {
        this.responsable.ville = id;
    
      }
    
      curUser: any = this.responsable[0];

我的API数据:

"@context": "/api/contexts/Ville",
    "@id": "/api/villes",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/villes/6",
            "@type": "Ville",
            "nomVille": "Davidboeuf"
        },
        {
            "@id": "/api/villes/7",
            "@type": "Ville",
            "nomVille": "Benoitboeuf"
        }];

我想你正在尝试做这样的事情:

<div *ngFor="let item of object | keyvalue">
  {{item.key}}:{{item.value}}
</div>

这个问题在这里也得到了很好的回答:

How to loop over object properties with ngFor in Angular

If you are using *ngFor, it only supports data is of type array or iterables only.

在您的场景中,变量 dropDownList 中的数据是对象类型或者可能为空 |不明确的。因此发生此错误。在使用该数据之前对其进行控制台处理。