2017-06-22 15 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:私は、パラメータとしてcompleteMethodにモデルを渡す

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

注意してください。
ああ、私は角2

Tsのためtypescriptですを使用していますによって:

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 = []; 
    } 
} 

はそれが役に立てば幸い!

関連する問題