您好,我是新来的角度,并开始学习。问:我有两个组件,一个是从angular material中选择下拉列表,另一个组件是从angular material.so中选择表格。当我从下拉列表中进行选择时,我希望在表格组件中看到所选值。这是我的代码。不知何故,当我进行选择时,服务正在调用该方法并侦听,但在那之后,我期待另一个方法执行,该方法订阅侦听更改。(如果我没有使用正确的术语,请道歉)
以下是组件的结构。
navbarparent.component.html (These are 2 main components)
<div>
<app-mr-navbar> <!-- navbar.component.html --> </app-mr-navbar>
<app-player-overview> <!-- Tablecomponent.html --> </app-player-overview>
</div>选择组件是组件的一个子组件。就是有下拉列表。
navbar.component.html
<div class="nav-header ">
<mat-grid-list cols="7">
<div>
<mat-grid-tile>
<app-eyefilter-navbar> <!-- selectbox.component.html --> </app-eyefilter-navbar>
</mat-grid-tile>
</div>
</mat-grid-list>
</div>这是selectbox.component.html
<div class="eyefilter-dd inline-style eachSelect">
<mat-form-field>
<mat-select (selectionChange)="selectionChange($event.value)" placeholder="EyeFilter" [formControl]="eyefilters" multiple id="eyeFilter">
<mat-option *ngFor="let filter of filterList" [value]="filter" class="custom-font-size">{{filter}}</mat-option>
</mat-select>
</mat-form-field>
</div>这是我的公共服务文件。
commonservice.ts
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { Observable} from 'rxjs/index';
import { BehaviorSubject, Subject} from 'rxjs/index';
@Injectable()
export class PlayerSkillsService {
private subject = new Subject<any>();
onChangeSelect(val: string) {
this.subject.next(val);
}
getSelectedDropdown(): Observable<any> {
return this.subject.asObservable();
}
}这是选择框组件ts文件。
selectbox.component.ts
import {PlayersService} from '../../services/playerskills.service';
@Component({
selector: 'app-eyefilter-navbar',
templateUrl: './eyefilter.component.html',
styleUrls: ['eyefilter.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class EyefilterComponent {
eyefilters = new FormControl();
filterList: string[] = ['Height', 'age', 'Show Power' , 'Finishing', 'Agility'];
constructor(private playerservice: PlayerSkillsService) {}
selectionChange(selectedVal): void { // This is executing
this.playerservice.onChangeSelect(selectedVal[0]);
}
}这是表格组件ts文件。
import {Component, OnInit, ViewChild, ViewEncapsulation, Input, OnChanges, SimpleChanges} from '@angular/core';
import {Subscription} from 'rxjs/index';
import {PlayerSkillsService} from '../../services/playerskills.service';
export class PlayersDataComponent implements OnInit {
selectedDropdownVal: any;
subcription: Subscription;
constructor(private playerservice: PlayerSkillsService) {
this.subcription = this.playerservice.getSelectedDropdown().subscribe(selectedDropdownVal => this.selectedDropdownVal = selectedDropdownVal);
// expecting selectedDropdownVal value to be set when you change the selection from selectbox.
// THIS IS NOT WORKING.
}
ngOnInit() {
this.subcription = this.playerservice.getSelectedDropdown().subscribe(selectedDropdownVal => this.selectedDropdownVal = selectedDropdownVal);
}
}我不确定我在这里遗漏了什么。有人能帮上忙吗?如果需要更多信息,请让我知道。谢谢
发布于 2019-02-21 03:56:47
您必须在包含下拉菜单的组件中使用EventEmitter:https://angular.io/api/core/EventEmitter
然后,在另一个组件上,您必须捕获$event。下面是一个完整的示例:https://www.infragistics.com/community/blogs/b/infragistics/posts/understanding-output-and-eventemitter-in-angular
发布于 2019-02-21 06:17:59
感谢@Ionut Tepus。这很有帮助。我错过了这个单行函数来捕获playertable组件中选定的DropDown值。
this.data.currentMessage.subscribe(message => {
this.message = message;
this.filterBasedonSelection();
console.log('message', message);
});https://stackoverflow.com/questions/54793058
复制相似问题