我正在尝试使用rxjs fromEvent从MatSelect中获取值。我无法得知事件已经发生。我将其与MatInput一起使用,因为我认为它继承自HTML输入元素的默认行为,但MatSelect有所不同。
如果我将mat-select换成默认选择,则会拾取(change)事件,并且该事件将按预期工作。(更改)在MatSelect上的某个位置显然已被弃用,并且不再起作用。
我已经尝试将模板ID移到mat-option上,以查看它是否可以从那里获取事件,没有运气。尝试过:(更改)(选择)(selectionChange)(valueChange)
@ViewChild('projectNameInput', {static: false, read: ElementRef}) projectNameInput: ElementRef;
@ViewChild('pipelineSelect', {static: false, read: ElementRef}) pipelineSelect: ElementRef;
@ViewChild('codeCoverageInput', {static: false, read: ElementRef}) codeCoverageInput: ElementRef;
ngAfterViewInit() {
// server-side search
const projectObs$ = fromEvent(this.projectNameInput.nativeElement, 'keyup');
const selectObs$ = fromEvent(this.pipelineSelect.nativeElement, 'onSelectionChange');
const coverageObs$ = fromEvent(this.codeCoverageInput.nativeElement, 'keyup');
const all$ = merge(projectObs$, selectObs$, coverageObs$)
.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => {
this.paginator.pageIndex = 0;
this.loadSummariesPage();
})
)
.subscribe();
this.paginator.page
.pipe(
tap(() => this.loadSummariesPage())
)
.subscribe();
}
------------------------------------
<mat-form-field appearance="fill">
<mat-label>types</mat-label>
<mat-select name="type" [(ngModel)]="type">
<mat-option #x *ngFor="let option of options" [value]="option.value">
{{option.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
如果您有Observable
,则要接收流数据就需要订阅它。
在您的组件代码中,您需要类似:
selectObs$.subscribe(selectedVal => console.log(selectedVal));
note:提醒您在OnDestroy
组件事件中退订,以防止内存泄漏。