【问题标题】:Issue with Ngfor only supports binding to Iterables such as ArraysNgfor 的问题仅支持绑定到可迭代对象,例如数组
【发布时间】:2026-01-12 11:30:01
【问题描述】:

错误 支持“string”类型的对象“活动”。 NgFor 只支持绑定到数组等Iterables。

我试图让它每次单击切换时都会将{{ setting }} 从非活动更改为活动。如果另一种方法不可行,则试图使此方法起作用。

代码html:

<ion-item>
    <ion-label>Front Door</ion-label>
    <ion-toggle [(ngModel)]="fdoor" ng-model="ni_toggle" (ionChange)="Fdoor()" (click)="change()"></ion-toggle>
  </ion-item>
 <div *ngFor="let setting of sett">
   {{ setting }}
 </div>

ts:

active: string = "Active"
inactive: string = "Inactive"


change() {
    if(this.fdoor = true) {
      this.sett = this.active
    }
  }

【问题讨论】:

    标签: arrays string ngfor ionic4


    【解决方案1】:

    我不知道你的目的是什么。但我认为你应该这样做:

    sett = false;
    change() {
        if(this.fdoor = true) {
          this.sett = !this.sett;
        }
      }
    

    HTML

    <ion-item>
        <ion-label>Front Door</ion-label>
        <ion-toggle [(ngModel)]="fdoor" ng-model="ni_toggle" (ionChange)="Fdoor()" (click)="change()"></ion-toggle>
      </ion-item>
     <div>
       {{ sett ? "Active" : "Inactive" }}
     </div>
    

    【讨论】:

    • 谢谢!我的目的基本上是按下一个开/关开关,输出显示它是开还是关。
    • 那我觉得你需要了解ngForngIf :)