简体   繁体   English

将数据绑定到另一个组件,然后使用@ Input,@ Output和EventEmitter以成角度的方式使用它

[英]Binding data to another component and use it in angular using @Input, @Output and EventEmitter

I am trying to pass the one team from array of team in another component and use it. 我正在尝试将一组团队中的一个团队传递给另一个组件,并使用它。 I am getting selected team in same component(team) as console.log but not getting in the another component(team-detail) 我在与console.log相同的组件(团队)中选择了团队,但没有在另一个组件中(团队详细信息)

Actually, I want to use team id to fetch details of other detail about team from API. 实际上,我想使用团队ID从API获取有关团队的其他详细信息。 please help me around this TIA 请帮助我解决这个TIA

Team.component.ts Team.component.ts

    export class TeamsComponent implements OnInit {

  @Output() selectedTeam = new EventEmitter<any>();

  constructor(private general: GeneralService) {
  }

  teamsObject: any;
  teams: [];

  ngOnInit() {
    this.loadTeams();
  }

  loadTeams() {
    this.general.getTeams().subscribe(data => {
      this.teamsObject = data;
      this.teams = this.teamsObject.teams;
    });
  }

  onSelectTeam(team: any) {
    this.selectedTeam.emit(team);
  }

}

Team.component.html Team.component.html

<div class="container-fluid " >
<div class="row " >
  <div class="col-xs-12" *ngFor="let team of teams">
    <div class="card border-dark " style="width: 250px; height: 450px; margin: 10px;" (click)="onSelectTeam(team)">
      <img class="card-img-top embed-responsive" src="{{team.crestUrl}}" alt="Card image cap">
      <div class="card-body">
        <h5 class="card-title">{{team.name}}</h5>
        <p class="card-text">{{team.address}}</p>
        <a href="{{team.website}}" class="btn btn-primary" target="_blank">Visit Website</a>
      </div>
    </div>
  </div>
</div>
  <app-team-detail *ngIf="selectedTeam" [team]="selectedTeam"></app-team-detail>
</div>

Team-detail component 团队细节部分

export class TeamDetailComponent implements OnInit {

  @Input() team: any;

  constructor() {
  }

  ngOnInit() {
  }

}

team-detail template(this template only for demo purpose.) 团队详细信息模板(此模板仅用于演示目的。)

<p>
  {{team.name}}
  {{team.crestUrl}}
  {{team.address}}
  {{team.website}}
</p>

“Output” identifies the events a component can fire to send information up the hierarchy to its parent. “输出”标识组件可以触发以将层次结构中的信息发送到其父级的事件。 In you case you want to send information from parent component to child. 在这种情况下,您希望将信息从父组件发送到子组件。 So you don't need to use Output. 因此,您无需使用Output。

Team.component.ts Team.component.ts

export class TeamsComponent implements OnInit {

  selectedTeam:any;

  constructor(private general: GeneralService) {
  }

  teamsObject: any;
  teams: [];

  ngOnInit() {
    this.loadTeams();
  }

  loadTeams() {
    this.general.getTeams().subscribe(data => {
      this.teamsObject = data;
      this.teams = this.teamsObject.teams;
    });
  }

  onSelectTeam(team: any) {
    this.selectedTeam = team;
  }

}

team.component.html team.component.html

 <app-team-detail *ngIf="selectedTeam" [team]="selectedTeam"></app-team-detail>

team-details template 团队细节模板

<p *ngIf="team">
  {{team.name}}
  {{team.crestUrl}}
  {{team.address}}
  {{team.website}}
</p>

create your property with getter and setter in you child component , rest of code remain as is 在子组件中使用getter和setter创建属性,其余代码保持原样

_team: any;
get team(): any {
    return this._team;
}

@Input()
set team(value: any) {
    this._team = value;
}

in child 在孩子

<p *ngIf="team">
  {{team.name}}
  {{team.crestUrl}}
  {{team.address}}
  {{team.website}}
</p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:如何使用 EventEmitter 将字符串值发送到另一个组件? - Angular: How to send string values to another component using EventEmitter? 如何在 Angular 6 中使用另一个组件中的一个组件数据? - How to use one component data in another component in angular 6? 输入和输出(使用角度) - Input and Output (using angular) 使用@output将角度从子组件发送到父组件 - angular using @output to sent data from child to parent component 有没有办法在另一个组件中使用变量 Angular 组件,并将数据传递给该变量组件? - Is there a way to use a variable Angular component inside another component, and also pass data to that variable component? 绑定HTML页面中组件的数据:Angular 6 - binding the data from component in HTML page : Angular 6 ngFor 无法通过与 @Input 的组件绑定来迭代另一个组件 - ngFor not able to iterate in another component through component binding with @Input 使用 Angular 中的输入绑定使用 if 语句进行验证 - Validation using if statement using input binding in Angular 如何基于单击将一个组件中存在的 function 的相同功能用于另一个组件以在 angular 中的两个组件中使用相同的 class 绑定 - How to use same functionalities of function present in one component to another based on click to use same class binding in both components in angular Angular 8 中 *ngFor 中 HTML 输入中的数据绑定 - Data binding in HTML input in *ngFor in Angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM