2017-06-22 29 views
1
<p-autoComplete [style]="{'width':'100%'}" name="searchSuggestions" 
[(ngModel)]="suggestion" (completeMethod)="searchSuggestions($event)" 
[suggestions]="searchSuggestionsResult" field="field"></p-autoComplete> 

我正在使用p-autoComplete。在使用鼠標複製和粘貼文本時,模型值(建議)未定義。如何從模型中獲取粘貼值?PrimeNG不更新p-autoComplete的型號值

+0

我敢肯定,如果您正在粘貼文本,它不會註冊爲「選擇」事件,因爲您實際上沒有使用自動完成功能您描述的複製和粘貼的用例更適合到: '' –

+0

我需要自動建議@David – Veera

回答

0

將字符串複製並粘貼到p-autoComplete對我來說工作正常。我只是想分享我的方法,希望你能找到點亮的方法。

HTML:

<p-autoComplete [style]="{'width':'100%'}" name="searchSuggestions" 
     [(ngModel)]="suggestion" (completeMethod)="searchSuggestions(suggestion)" 
     [suggestions]="searchSuggestionsResult" field="field"> 
</p-autoComplete> 

請注意,我傳遞的模式,以completeMethod作爲參數。
OH中,我使用的打字稿對角2

TS方式:

searchSuggestions(suggestion) { 
    this.searchSuggestionsResult = []; 

    if(suggestion != "") { 

     var list = this.searchSuggestionsResult.filter(function(el) { 
      return (el) ? el.toLowerCase().startsWith(suggestion.toLowerCase()) : false; 
     }); 

     list.sort((a,b) => a.toLowerCase().localCompare(b.toLowerCase()); 
     list.forEach(element => { 
      this.searchSuggestionsResult.push(element); 
     }); 
    } else { 
     this.searchSuggestionsResult = []; 
    } 
} 

希望它能幫助!