我目前正在对Angular 2使用ng2-completer(https://github.com/oferh/ng2-completer),并努力添加建议以得到完整的响应,而不只是一个值。

另外,选择建议后,如何分配方法来处理此建议?

到目前为止,我得到的代码是:

import { Component } from '@angular/core';
import { AutoComplete } from './autocomplete';
import { CompleterService, CompleterData, RemoteData } from 'ng2-completer';
import { SearchComponent } from './search.component';
import { QueryService } from './query.service';


@Component({
  selector: 'app-home',
  template: `<ng2-completer [(ngModel)]="searchStr" [dataService]="dataService" [minSearchLength]="0" [inputClass]="['form-control input-list']" [autofocus]="['true']" [selected]="selected()"></ng2-completer>`,
  //directives: [ AutoComplete ]
})

export class HomeComponent {

  public searchStr: string;
  private dataService: CompleterData;

  constructor(private completerService: CompleterService, private queryService: QueryService) {
    //this.dataService = completerService.local(this.searchData, 'color', 'color');
    this.dataService = completerService.remote('http://localhost:61227/machine/?query=','ComputerHostname, AssetID', 'ComputerHostname').descriptionField('ComputerType');

//this.dataService = this.queryService.search(searchStr).then(items => this.items = items);

}

selected () {

console.log("test");

}
 }

但是,它显示以下错误:

无法绑定(bind)到“已选择”,因为它不是“ng2-completer”的已知属性。

最佳答案

selected是事件而不是属性,因此它的语法(如Angular template syntax中所述)应为(selected)="selected($event)"autofocus需要一个布尔值(see in ng2-completer doc)而不是数组,因此您应该使用[autofocus]="true"

10-04 10:31