[英]Cannot find a differ supporting object '[object Object]' of type 'object'. - context menu issues
I have issues with context menu.我有上下文菜单的问题。 I am not sure why, I made similar thing many times but now I am unable to pass it.我不知道为什么,我做了很多次类似的事情,但现在我无法通过。 When I deleted this line:当我删除这一行时:
<div *ngFor="let element of contextMenu">
</div>
Context menu works fine (but I need this line to fill my context menu. This is part of html:上下文菜单工作正常(但我需要这一行来填充我的上下文菜单。这是 html 的一部分:
<mat-menu #contextMenu="matMenu">
<ng-template matMenuContent>
<div *ngFor="let element of contextMenu">
</div>
</ng-template>
</mat-menu>
And my context menu object:我的上下文菜单对象:
@Input() contextMenu: MenuElement[];
And html with filled input:和填充输入的html:
<lib-mp-file-explorer
[fileElements]="fileElements | async"
[path]="currentPath"
[canNavigateUp]="canNavigateUp"
[topMenu]="topMenu"
[contextMenu]="contextMenu"
(folderAdded)="addFolder($event)"
(elementRemoved)="removeElement($event)"
(navigatedDown)="navigateToFolder($event)"
(navigatedUp)="navigateUp()"
(elementRenamed)="renameElement($event)"
(elementMoved)="moveElement($event)"
>
</lib-mp-file-explorer>
And my construction:而我的建设:
this.contextMenu = [
{
name: 'Open in Mark',
icon: 'M',
action: undefined,
disabled: true
},
...
]
I hate this error now:我现在讨厌这个错误:
core.js:5828 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. core.js:5828 错误错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor only supports binding to Iterables such as Arrays. NgFor 仅支持绑定到可迭代对象,例如数组。
But my array is iterable TT但我的数组是可迭代的 TT
Your menu reference variable has the same name..您的菜单参考变量具有相同的名称..
<mat-menu #contextMenu="matMenu">
..as the array you are trying to loop through ..作为您尝试循环的数组
<div *ngFor="let element of contextMenu">
A template reference variable is not iterable.模板引用变量不可迭代。
Try renaming your array:尝试重命名您的数组:
<div *ngFor="let element of menuElements">
And Input()
:和Input()
:
@Input() menuElements: MenuElement[];
and then进而
this.menuElements = [
// your data
]
You can check what there are actual values in your contextMenu
:您可以检查contextMenu
有哪些实际值:
<p>contextMenu {{ contextMenu | json }}
However, your error says that it is object, so you can use keyvalue
pipe since Angular 6:但是,您的错误表明它是对象,因此您可以从 Angular 6 开始使用keyvalue
管道:
<div *ngFor="let item of contextMenu | keyvalue">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.