简体   繁体   English

使用异步管道,angular2无法正常工作

[英]using async pipe with angular2 not working

Whenever i use the async pipe i am getting console errors. 每当我使用异步管道时,我都会遇到控制台错误。

   <component *ngFor="#c of columns | async" [column]="c"></component>

I get this console error: 我收到此控制台错误:

EXCEPTION: Invalid argument '[object Object]' for pipe 'AsyncPipe' in [columns | EXCEPTION:[columns |中的管道'AsyncPipe'的参数'[object Object]'无效 async in Projects@1:51] browser_adapter.js:76 EXCEPTION: Invalid argument '[object Object]' for pipe 'AsyncPipe' in [columns | 在Projects @ 1:51中的异步] browser_adapter.js:76 EXCEPTION:[columns |'|'|'| | | | | | | | | async in Projects@1:51] Projects @ 1:51中的异步]

Is there something missing? 有什么遗失的吗?

The columns attribute must be either an observable or a promise (not an array or something else. See this line in the source code of Angular2 ( AsyncPipe class): https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/async_pipe.ts#L109 . columns属性必须是observable或promise(不是数组或其他东西。请参阅Angular2( AsyncPipe类)源代码中的这一行: https//github.com/angular/angular/blob/master/modules /angular2/src/common/pipes/async_pipe.ts#L109

In the case of an HTTP call for example, you need to set the columns attribute with the returned observable directly. 例如,在HTTP调用的情况下,您需要直接使用返回的observable设置columns属性。 The async will be responsible to handle it, ie calling its subscribe method, dispose it. async将负责处理它,即调用它的subscribe方法,处理它。 Here is a sample: 这是一个示例:

this.columns = 
   this.http.get('https://angular2.apispark.net/v1/companies/', {
     headers: headers
   }).map(res => res.json());

Instead of setting the `columns attribute from a subscribe method you manage by yourself: 而不是从您自己管理的订阅方法设置`columns属性:

this.http.get('https://angular2.apispark.net/v1/companies/', {
  headers: headers
})
.map(res => res.json())
.subscribe(data => this.columns = data);

Hope it helps you, Thierry 希望它对你有帮助,蒂埃里

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM