2016-10-10 14 views
9

Başka bir bileşen <inner-component> sarar ve InnerComponent.innerChanged() özel etkinliğin bağlanan bir bileşen var. Ben bir @output özelliğini kullanarak kabarcıklandırmak istiyorum, ama ben de çıktıyı geri yüklemek istiyorum.Bir iç bileşenin @Output değerini nasıl geri yüklerim?

Bunu yapmak için RxJS.debounce() veya .debounceTime()'u nasıl kullanırım? Böyle

şey:

import {Component, Output, EventEmitter} from 'angular2/core'; 
import 'rxjs/add/operator/debounce'; 
import 'rxjs/add/operator/debounceTime'; 

@Component({ 
    selector: 'debounced-component', 
    template: ` 
    <div> 
     <h1>Debounced Outer Component</h1> 
     // export class InnerComponent{ 
     // @Output() innerChanged: new EventEmitter<string>(); 
     // onKeyUp(value){ 
     //  this.innerChanged.emit(value); 
     // } 
     // } 
     <input #inner type="text" (innerChange)="onInnerChange(inner.value)"> 
    </div> 
    ` 
}) 
export class DebouncedComponent { 
    @Output() outerValueChanged: new EventEmitter<string>(); 

    constructor() {} 

    onInnerChange(value) { 
    this.outerValuedChanged.emit(value); // I want to debounce() this. 
    } 
} 
+0

import { Component, Input, Output, EventEmitter, ViewChild, OnDestroy } from '@angular/core'; import { Subject } from 'rxjs/Subject'; import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/operator/debounceTime'; @Component({ selector: 'app-searchbox', template: ' <input type="text" autocomplete="off" [(ngModel)]="query" [placeholder]="placeholder" (change)="onInputChange($event)"> ' }) export class SearchboxComponent implements OnDestroy { @Input() debounceTime = 500; @Output() change = new EventEmitter<string>(); query = ''; placeholder = 'Search...'; results; debouncer = new Subject<string>(); subs = new Array<Subscription>(); constructor() { this.subs.push(this.debouncer.debounceTime(this.debounceTime).subscribe( (value: string) => { this.change.emit(value); }, (error) => { console.log(error); } )); } onInputChange(event: any) { this.debouncer.next(event.target.value); } ngOnDestroy() { for (const sub of this.subs) { sub.unsubscribe(); } } }
ait kopya gibi görünüyor [Bu soru] (http://stackoverflow.com/a/36849347/2435473) –

+0

ben kullanamıyorum çünkü aynı olduğunu sanmıyorum 'Observable.fromEvent()' ve bir 'FormControl.valueChanges' yok. 'This.outerValuedChanged.debounce (500) .emit (değer)' çalışmak için görünmüyor ... – michael

cevap

22

Bir Konu kullanabilirsiniz değerlerini sektirilmesini belirleyin. Bir konu hem gözlemlenebilir hem de gözlemci. Bu, onu gözlenebilir bir değer olarak ele alabilmeniz ve ona değer biçebilmeniz anlamına gelir.

Bunun ve gürültüye bu şekilde iç-bileşeninden yeni değerleri eklemek için bu kaldıraç olabilir.

export class DebouncedComponent { 
    @Output() outerValueChanged: new EventEmitter<string>(); 
    const debouncer: Subject = new Subject(); 

    constructor() { 
     // you listen to values here which are debounced 
     // on every value, you call the outer component 
     debouncer 
     .debounceTime(100) 
     .subscribe((val) => this.outerValuedChanged.emit(value)); 
    } 

    onInnerChange(value) { 
    // send every value from the inner to the subject 
    debouncer.next(value); 
    } 
} 

Bu denenmemiş sözde koddur. Buradaki konseptin çalışma örneğini görebilirsiniz (http://jsbin.com/bexiqeq/15/edit?js,console). Açısal değil ama konsept aynı kalıyor. İşte

+1

teşekkür ederim! Ben sadece özel filtreleyicidir 'geçmek zorunda kaldı: Konu = new Konu ();' TS mutlu etmek için. – michael

+0

Bazı nedenlerden dolayı Angular2 üzerinde çalışmaz (github sorunlarını kontrol edin). Bu şekilde deneyin: https://stackoverflow.com/questions/32051273/angular2-and-debounce (değer herhangi gürültüye olmadan tetiklenir) –

+0

mükemmel güzel açısal 2 için çalışıyor. Teşekkür ederim! – Baso

0

tüm gerekli ithalat ile bir çalışma örneği Sınıf, Açısal 4+ :) typescript ve tslint dostu bazı insanlar anlar önce aradığı şeyi arayan yardımcı olabileceğini düşündüm olduğunu!

İlgili konular