简体   繁体   English

在angular2中找不到不同的支持对象,异步管道和ngFor问题

[英]Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

I have component that takes as input array of objects that should be filtered and displayed in the template. 我有一个组件,它作为对象的输入数组,应该过滤并显示在模板中。

  @Input() inputAddons: Array<InputAddon>;
  addOns: Observable<InputAddon>;
  lblLeftAddons: Observable<InputAddon>;

The definition in the class is above. 班上的定义如上。

ngOnInit(): void {
this.addOns = Observable.from(this.inputAddons);
this.lblLeftAddons = this.addOns.filter(function (x){
  return x.pos == 'left' && x.type == 'label'}
);

this.lblLeftAddons.subscribe(x => console.log(x));
}

this is the code where i filter the array 这是我过滤数组的代码

in the template i have this code 在模板中我有这个代码

*ngFor="#addon of lblLeftAddons | async"

but it doesn't work. 但它不起作用。 I get exception 我得到例外

Cannot find a differ supporting object '[object Object]' in [lblLeftAddons | async in InputText@14:10]

Any ideas what i do wrong? 我有什么想法吗?

I solved it with few modifications on my code. 我解决了它,对我的代码进行了一些修改。 First i replaced 首先我换了

lblLeftAddons: Observable<InputAddon>;

with

lblLeftAddons: Observable<InputAddon[]>;

Observable must return array in order ngFor to work. Observable必须返回数组才能使ngFor工作。

And i modified addon observable to return array 我修改了addon observable来返回数组

this.lblLeftAddons = this.addOns.filter(function (x){

 return x.pos == 'left' && x.type == 'label'}
);

Also i added changeDetection: ChangeDetectionStrategy.OnPush in my Component definition 我还在我的组件定义中添加了changeDetection: ChangeDetectionStrategy.OnPush

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2找不到类型为“字符串”的其他支持对象。 NgFor仅支持绑定到可迭代对象,例如数组 - Angular2 Cannot find a differ supporting object of type 'string'. NgFor only supports binding to Iterables such as Arrays Angular ngFor无法找到其他支持对象&#39;[object Object]&#39; - Angular ngFor Cannot find a differ supporting object '[object Object]' 找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables,例如带有异步管道的数组 - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays with Async pipe Angular2:找不到与服务类不同的支持对象 - Angular2: Cannot find a differ supporting object from service class 找不到其他支持对象Error Angular2 - Cannot find a differ supporting object Error Angular2 «Cannot find a different support object '[object Object]'» with Observable when `*ngFor="let item of items | async" ` - «Cannot find a differ supporting object '[object Object]'» with Observable when `*ngFor="let item of items | async" ` Angular 7 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Iterables,比如 Arrays - Angular 7 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 角度-找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组 - Angular - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Angular 8 中的数组 - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays in angular 8 Angular:*ngFor 循环给我错误:找不到类型为“object”的不同支持对象“[object Object]” - Angular: *ngFor loop giving me error: Cannot find a differ supporting object '[object Object]' of type 'object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM