首页 > 解决方案 > 角度 ngFor 警告 - 只允许使用数组和可迭代对象

问题描述

需要帮助理解此错误消息,
在 UI 方面一切似乎都正常,所以我不确定为什么我会看到这个。

Error trying to diff '[object Object]'. Only arrays and iterables are allowed

这是 for 循环:

    <tr *ngFor="let draft of drafts; let i = index">
        <td  width="1%">                        
         <xerox-checkbox [(ngModel)]="draft.selected" ></xerox-checkbox>
        </td>     
        <td>
   code truncated. 

和错误消息:

DraftsComponent.html:29 ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed
    at DefaultIterableDiffer.push../node_modules/@angular/core/fesm5/core.js.DefaultIterableDiffer.diff (core.js:5642)
    at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3157)
    at checkAndUpdateDirectiveInline (core.js:9246)
    at checkAndUpdateNodeInline (core.js:10507)
    at checkAndUpdateNode (core.js:10469)
    at debugCheckAndUpdateNode (core.js:11102)
    at debugCheckDirectivesFn (core.js:11062)
    at Object.eval [as updateDirectives] (DraftsComponent.html:96)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
    at checkAndUpdateView (core.js:10451)

草稿对象数组:

drafts[{"id":"1000019","printerName":"Regin1","printerState":"US","userId":"userId1","userEmail":null,"aaName":"","sale":null},
           {"id":"1000018","printerName":null,"printerState":"US","userId":"userId2","userEmail":null,"aaName":"","sale":null},
           {"id":"1000014","printerName":"my new entry","printerState":"US","userId":"userId4","userEmail":null,"aaName":"","sale":null}]

标签: angularjs

解决方案


在您使用的模板drafts[_].selected上,但该属性在变量的分片日志中不存在。

你能提供更多细节吗?和/或一个https://stackblitz.com/玩?

我制作了一个最小的示例:
https ://stackblitz.com/edit/angular-ivy-zzdpk3?file=src/app/app.component.html

PS:什么是adp-checkbox?我在谷歌上没有找到任何关于它的信息。


推荐阅读