获取无法找到'object'类型的不同支持对象'[object Object]'。 NgFor仅支持绑定到诸如Arrays之类的Iterables

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取无法找到'object'类型的不同支持对象'[object Object]'。 NgFor仅支持绑定到诸如Arrays之类的Iterables相关的知识,希望对你有一定的参考价值。

我想在文本框中实现自动完成功能,因为我创建了Web API,它返回了Json格式文件,但我得到“找不到'对象'类型的不同支持对象'[object Object]'。NgFor只支持绑定到Iterables比如Arrays。“请帮帮我

我的代码:=

服务

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
@Injectable()
export class ClientSearchService {

endPoint: string;
constructor(private http: Http) {
    this.endPoint = "http://localhost:57888/api/Employees/GetEmployeeById";
}
search(term: string): Observable<any[]> {
    var ClientList = this.http.get(this.endPoint + '/' + term)       
        .map((r: Response) => { return (r.json().length != 0 ? r.json() : [{ "ClientId": 0, "ClientName": "No Record Found" }]) as any[] });        
        return ClientList;
}
}

app component.ts

    import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
    import { Observable, Subject } from 'rxjs';
    import { ClientSearchService } from './_services/client-search.service';
    @Component({
      selector: 'client-search',
      templateUrl: './app.component.html',
    })
    export class AppComponent implements OnInit {
      public clients: Observable<any[]>;
      private searchTerms = new Subject<string>();
      public ClientName = '';
      public flag: boolean = true;
      constructor(
        private clientSearchService: ClientSearchService,
      ) {

      }

      ngOnInit(): void {
        this.clients = this.searchTerms
          .debounceTime(300)    
          .distinctUntilChanged()  
          .switchMap(term => term   
            ? this.clientSearchService.search(term)
            : Observable.of<any[]>([]))
          .catch(error => {
            console.log(error);
            return Observable.of<any[]>([]);
          });
      }
      searchClient(term: string): void {
        this.flag = true;
        this.searchTerms.next(term);
      }
      onselectClient(ClientObj) {   
        if (ClientObj.ClientId != 0) {
          this.ClientName = ClientObj.ClientName;     
          this.flag = false;
        }
        else {
          return false;
        }
      }

    }

HTML

        </div>        
    </div>
    <div class="search-result" *ngIf="flag">
        <ul>
            <li *ngFor="let client of clients | async">
                //<a (click)="onselectClient(client)">{{client.ClientName}}</a>
            </li>           
        </ul>
    </div>

我的json文件如下

{“ClientId”:15,“ClientName”:“Abhinav Singh”}

答案

由于错误说你的结果客户不是array,它的objectngFor工作在一系列Objects

要修复它,请将您的JSON文件更改为

[{“ClientId”:15,“ClientName”:“Abhinav Singh”}]

以上是关于获取无法找到'object'类型的不同支持对象'[object Object]'。 NgFor仅支持绑定到诸如Arrays之类的Iterables的主要内容,如果未能解决你的问题,请参考以下文章

'无法找到该进程类型'heroku节点部署

引用类型''声明它在'System.Web'中定义,但无法找到它

无法找到'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'类型的bean

离子错误:尝试diff'[object Object]'时出错

需要一个无法找到的'com.example.dao.InterfaceName'类型的bean

EXTJS,SERVLET后台返回数据火狐无法获取,且报错:uncaught exception: [object Object]