代码之家  ›  专栏  ›  技术社区  ›  mamesaye

JSON对象的角4型前置反应式数组

  •  0
  • mamesaye  · 技术社区  · 7 年前

    我有一个窗体,它有一个输入字段,可以输入一个类型。我正在从API调用中获取一个JSON对象数组:

    allPeople = [{"id":1,"first_name":"Abba","last_name":"Lee","current_position":"Travel Expert"}, {"id":2,"first_name":"Abbey","last_name":"Joe","current_position":"water keeper"}]    
    

    但是当我开始打字的时候 [object object]

    这就是我现在拥有的:

    <input formControlName="person" [typeahead]="allPeople" [typeaheadOptionsLimit]="10" [typeaheadMinLength]="0" type="text">   
    

    我尝试过:

    <input formControlName="person" [typeahead]="inf as inf.first_name for inf in allPeople | filter:{first_name:$viewValue}" [resultFormatter]="formatMatches" [inputFormatter]="formatMatches"  [typeaheadOptionsLimit]="10" [typeaheadMinLength]="0" type="text">   
    

    但我得到了一个错误:

    Uncaught Error: Template parse errors:
    Parser Error: Unexpected token 'as' at column 5 in [inf as inf.first_name for inf in allPeople | filter:{first_name:$viewValue}] in ...   
    

    当我开始输入时,如何获取输入以显示/建议我的名字和姓氏?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Paresh Gami    7 年前

    您的数组是自定义数组,因此需要指定需要在打印头中显示的键

    <input [(ngModel)]="customSelected"
           [typeahead]="allPeople"
           typeaheadOptionField="first_name"
           class="form-control">
    

    你需要使用 typeaheadOptionField 在这里,你需要给键名值在哪里,所以在这里你必须使用 first_name