InvalidPipeArgument:' [object Object],用于管道' AsyncPipe'

时间:2018-06-04 14:49:35

标签: angular

我遇到了异步管道和观察者组合的问题。我一直在尝试使用' player'和app.component创建一个team.service,它将获得该数据。我收到以下错误:

  

InvalidPipeArgument:' [object Object],[object Object],[object Object]' for pipe' AsyncPipe'

更多,当我擦除异步管道时,它可以工作。有人知道如何解决?我的代码

team.service:

export class TeamService {

  fillPlayers(): any {
    return [
      { 'name': 'Buzz', 'surname': 'Astral' },
      { 'name': 'Mad', 'surname': 'Max' },
      { 'name': 'Eazy', 'surname': 'E' }
    ];
  }

  constructor() {}

  getPlayersWithObservable(): Observable<TeamInterface[]> {
    return this.fillPlayers();
  }
}

app.component.ts

export class AppComponent {

  observableTeam: Observable<TeamInterface[]>;

  constructor(private _teamService: TeamService) {}

  ngOnInit() {
    this.observableTeam = this._teamService.getPlayersWithObservable();
  }
}

app.component.html

  <ul>
    <li *ngFor="let player of observableTeam | async">
      {{ player.name }}
    </li>
  </ul>

1 个答案:

答案 0 :(得分:1)

删除异步,你只返回一个不是一个可以想象的数组

<li *ngFor="let player of observableTeam">
      {{ player.name }}
 </li>

并将您的返回类型更改为

 getPlayersWithObservable(): any {
    return this.fillPlayers();
  }

 observableTeam: <TeamInterface[]>=[]>;