javascript - '[object 对象],[object 对象 ]' for pipe ' AsyncPipe'

标签 javascript angular typescript asynchronous angular-material

我需要使用 Angular 为 8 的 Angular Material 创建自动完成功能。

现在我在 ts 文件中使用这段代码:

 @Input() admins: User[];
  userGroupOptions: Observable<User[]>;
  filterFormFG: FormGroup;
  constructor(private utilService: UtilsService, private messageService: MessageService) {
    this.createForm();
    this.userGroupOptions = this.filterFormFG.get('createdByRefId').valueChanges
      .pipe(
        startWith(''),
        map(admin => admin ? this._filterStates(admin) : this.admins.slice())
      );
  }

  ngOnInit() {
    // tslint:disable-next-line: no-non-null-assertion


  }

  private _filterStates(value: string): User[] {
    const filterValue = value.toLowerCase();

    return this.admins.filter(state => state.fullname.toLowerCase().indexOf(filterValue) === 0);
  }

并在 html 文件中使用它:

          <mat-form-field class="example-full-width" appearance="outline">
                        <input matInput [placeholder]="'MESSAGES.SENDER' | translate" aria-label="'MESSAGES.SENDER' | translate" [matAutocomplete]="auto"
                        formControlName="createdByRefId">
                        <mat-autocomplete #auto="matAutocomplete">
                            <mat-option *ngFor="let item of (admins | async)" [value]="item.firstName + ' ' +item.lastName">
                               {{ item.firstName + ' '+ item.lastName | translate }}
                            </mat-option>
                        </mat-autocomplete>
                    </mat-form-field>

但它告诉我这个错误:

ERROR Error: InvalidPipeArgument: '[object Object],[object Object]' for pipe 'AsyncPipe' at invalidPipeArgumentError (common.js:4800)

有什么问题吗?我该如何解决这个问题?????

最佳答案

async pipe 订阅 Observable 或 Promise 并返回它发出的最新值。当发出新值时,异步管道标记要检查更改的组件。当组件被销毁时,异步管道会自动取消订阅以避免潜在的内存泄漏。

你不需要在这里使用异步管道只是删除它,admins 只是一个对象数组

<mat-option *ngFor="let item of admins" [value]="item.firstName + ' ' +item.lastName">
  {{ item.firstName + ' '+ item.lastName | translate }}
</mat-option>

已更新!

userGroupOptions 与异步管道一起使用

<mat-option *ngFor="let item of userGroupOptions | async " 
             [value]="item.firstName + ' ' +item.lastName">
  {{ item.firstName + ' '+ item.lastName | translate }}
</mat-option>

关于javascript - '[object 对象],[object 对象 ]' for pipe ' AsyncPipe',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668303/

相关文章:

javascript - jquery 脚本无法在 HTML 中运行

javascript - 无法使用 Ionic 4+ 设置动画启动画面

angular - 是否有最佳实践来放置跨文件共享的全局变量?

angularjs - 在基类中使用依赖项而不必从继承类传递此依赖项

javascript - Electron 应用程序中出现 Uncaught Error : Bootstrap's JavaScript requires jQuery at bootstrap. min.js :6,

angular - 为什么我不能在取消订阅后再次订阅同一个 EventEmitter

javascript - 如何在 Angular 2 中使用 ngIf-else 中的表达式

javascript - Django调试工具栏错误路径

javascript - 使用 js regex 将简单的标记样式(如 **bold** 替换为 <b>bold</b>)

javascript - Jquery:从获取请求更改 jqXHR 的 HTML 内容