2017-03-13 7 views
1

"keyUp"이벤트에서 호출되는 함수를 디버깅하려면 어떻게해야합니까? 여기 각도 2 - keyUp 이벤트 디버깅

내 코드입니다 :

내 기능

private handleSearch(searchTextValue: string, skip?: number): void { 
    this.searchTextValue = searchTextValue; 
    if (this.skip === 0 || typeof skip === "undefined") { 
     this.skip = 0; 
     this.pageIndex = 1; 
    } else { 
     this.skip = skip; 
    } 
    this.searchTextChanged.emit({ searchTextValue: searchTextValue, skip: this.skip, take: this.itemsPerPage }); 
} 

내 HTML

<input type="text" class="form-control" placeholder="{{ 'searchquery' | translate }}" id="searchText" #searchText (keyup)="handleSearch(searchText.value)"> 

Bassically 내가 무엇을 달성하기 위해 노력하고있어 handleSearch이를 호출되는 것입니다 사용자가 타이핑을 중단 한 후 잠시.

나는 로다시의 _debounce()을 사용할 수 있다고 알았지 만, 이걸 내 keyUp 이벤트에 넣는 방법을 찾지 못했습니다.

답변

7

rxjs/Subject를 만들고 keyup에서 .next()를 호출하고 원하는 debounceTime으로 구독 할 수 있습니다.

올바른 방법인지는 모르겠지만 작동 여부는 확실하지 않습니다.

private subject: Subject<string> = new Subject(); 

ngOnInit() { 
    this.subject.debounceTime(500).subscribe(searchTextValue => { 
    this.handleSearch(searchTextValue); 
    }); 
} 

onKeyUp(searchTextValue: string){ 
    this.subject.next(searchTextValue); 
} 

HTML :

<input (keyup)="onKeyUp(searchText.value)"> 
+0

가 어떻게 ngOnInit()'에 매개 변수를 전달할 수 있습니다 { this.subject.debounceTime (500) .subscribe (고해상도 => {// 뭔가를 할}); }'? – Nicolas

+0

검색 쿼리를 onKeyUp()에 전달한 다음 .next()에 전달하면 구독에서 'res'변수가됩니다. 문자열로 작업 할 때 제목이 에서 제목이 으로 변경되었는지 확인하십시오. 대답을 수정하겠습니다. – Ploppy

+0

그리고 1 개의 선택적 매개 변수가 있다면? 내가 그들을 대상에 넣어야합니까? OP에서 나는'searchTextValue'와'skip'에 의존합니다. 이것은 선택 사항입니다 – Nicolas