首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用服务不工作的两个组件angular 6/7之间的交互

使用服务不工作的两个组件angular 6/7之间的交互
EN

Stack Overflow用户
提问于 2019-02-21 02:32:19
回答 2查看 161关注 0票数 3

您好,我是新来的角度,并开始学习。问:我有两个组件,一个是从angular material中选择下拉列表,另一个组件是从angular material.so中选择表格。当我从下拉列表中进行选择时,我希望在表格组件中看到所选值。这是我的代码。不知何故,当我进行选择时,服务正在调用该方法并侦听,但在那之后,我期待另一个方法执行,该方法订阅侦听更改。(如果我没有使用正确的术语,请道歉)

以下是组件的结构。

代码语言:javascript
复制
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>

选择组件是组件的一个子组件。就是有下拉列表。

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

这是我的公共服务文件。

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
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);
  }
}

我不确定我在这里遗漏了什么。有人能帮上忙吗?如果需要更多信息,请让我知道。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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

票数 1
EN

Stack Overflow用户

发布于 2019-02-21 06:17:59

感谢@Ionut Tepus。这很有帮助。我错过了这个单行函数来捕获playertable组件中选定的DropDown值。

代码语言:javascript
复制
this.data.currentMessage.subscribe(message => {
  this.message = message;
  this.filterBasedonSelection();
  console.log('message', message);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54793058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档