2017-06-22 45 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 = []; 
    } 
} 

希望它能帮助!