2016-12-19 48 views
0

可观察我编组多个输入和他们的分量模板像这样的活动:角2:从多个活动或事件处理

<tr (input)="onSearchObjectChange($event)"> 
    <th><input [(ngModel)]="searchObject.prop1"></th> 
    <th><input [(ngModel)]="searchObject.prop2"></th> 
    ... 
</tr> 

现在我想创建一个可观察出onSearchObjectChange()的,应得到一个承诺的结果。我该怎么做,或者有没有更好的方式来使用Observable?我是否也可以使用对象的distinctUntilChanged()函数?

我试图用一个模板变量来解决这个问题:

<tr #search > 
<th><input [(ngModel)]="searchObject.prop1"></th> 
<th><input [(ngModel)]="searchObject.prop2"></th> 
... 
</tr> 

而且随着ViewChild的帮助:

@ViewChild('search') search; 

ngAfterViewInit() { 
    Observable.fromEvent(this.search, 'input') 
     .subscribe(() => console.log("help")); 
} 

但它不工作...

回答

0
import {Component, AfterViewInit, ViewChild, ElementRef} from '@angular/core'; 
import {Observable} from "rxjs"; 

@Component({ 
    selector: 'search', 
    templateUrl: './search.html', 
    styleUrls: ['./search.css'], 
    providers: [] 
}) 
export class SearchComponent implements AfterViewInit { 
@ViewChild('search') search: ElementRef; 

constructor() { 
} 

ngAfterViewInit() { 
    Observable.fromEvent(this.search.nativeElement, 'input') 
     .subscribe(() => alert("It works!")) 
} 
}