2017-02-13 40 views
3

我正在嘗試使用ng2-auto-complete的列表格式化功能,但無法找到如何實現的一個很好的例子。我有以下代碼至今:如何使用ng2-auto-complete的list-formatter例子?

<input class="form-control input-list" ng2-auto-complete 
     [(ngModel)]="model4" 
     placeholder="Search" 
     [source]="googleGeoCode" 
     list-formatter="myListFormatter" 
     path-to-data="" 
     value-property-name=null 
     display-property-name=null 
     min-chars="2" 
     /> 

export class HomeComponent { 

    templateStr: any = templateStr; 
    valuePropertyName: string; 
    displayPropertyName: string; 

    googleGeoCode: string = "http://localhost:61227/machine/?query=:keyword"; 

    myListFormatter(data: any): string { 
     let html: string = ""; 
     html += data[this.valuePropertyName] ? `<b>(${data[this.valuePropertyName]})</b>` : ""; 
     html += data[this.displayPropertyName] ? `<span>${data[this.displayPropertyName]}</span>` : data; 
     return html; 
     } 

} 

所以,我怎麼能得到列表格式工作?

+0

我想你忘了問一個問題;) – Alex

+0

什麼是目前的行爲和預期的行爲是什麼? – Alex

回答

5

的列表格式屬性名稱必須是括號內,像這樣:

[list-formatter]="myListFormatter" 

在功能this.valuePropertyName是拿不到任何認可。你必須在你的數據對象中加入引號。

我認爲默認值是'id'和'value'。嘗試data['id']data['value']