あなたはこのようにそれを行うことができます:
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<div #editable contenteditable=true (keyup)='search(editable.innerText)'>test</div>
<div #editable contenteditable=true (keyup)='_searchSubject.next(editable.innerText)'>inline without search function</div>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
private results;
private _searchSubject = new BehaviorSubject<string>('');
ngOnInit() {
this._searchSubject
.debounceTime(200)
.distinctUntilChanged()
.subscribe(input => console.log(input));
// dont want to fake it.. :)
//.switchMap((query: any) => this.myService.searchUser(query))
//.subscribe((response: any) => {
// this.results = response.data;
//});
}
search(val: any) {
console.log('search', val);
this._searchSubject.next(val);
}
}
- 01:それ以外の場合はあなたが文句を言わない
- 最後のキーが
next
検索機能に取り組んでデモを購読して使用するためにグローバルな主題を使用し得る
(keyup)
代わりの
(keypress)
=>を使用します
"空" の入力をフィルタリングするためにUPDATE
、このようにそれを実行します。
ngOnInit() {
this._searchSubject
.debounceTime(200)
.distinctUntilChanged()
.filter(input => input) // BUT can lead to weird behaviors???!
.subscribe(input => console.log('"' + input + '"'));
// dont want to fake it.. :)
//.switchMap((query: any) => this.myService.searchUser(query))
//.subscribe((response: any) => {
// this.results = response.data;
//});
}
そして、問題は何ですか?そのため、サーバーをあまりにも頻繁にプールすることを避けるために 'debounceTime()'が必要です。あるいは以前の回答をすべて残したいと思っていますか? – martin
これは検索機能なので、あまりにも頻繁にプールするのを避けるためにdebouceTime()が使用されています。問題は、完全な単語を検索できるように以前のテープレターを残したいということです。 –
これで、ユーザーが 'query'で押された文字は1つだけです。 '.innerText'が' editable = true'コンポーネントで返ってくるものがわからないので、代わりに '.textContent'を使うことができますか?私は私が持っている受信したテキストログインすると例えば – martin