我有 3 个组件: 1. 客户列表 2. 客户(姓名) 3. 客户的完整信息(姓名、年龄、地址),当我单击用户项时必须出现这些信息。
而且我不明白,当我在客户端列表中单击此客户端时,如何获取有关特定客户端的信息(如何显示第三个组件)。
这是我的代码:
1) 客户列表.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { ClientsService } from '../clients.service';
import { Client } from '../../models/client.model';
@Component({
selector: 'app-clients-list',
templateUrl: './clients-list.component.html',
styleUrls: ['./clients-list.component.css']
})
export class ClientsListComponent implements OnInit {
public clients: Client[] = [];
public selectedClient;
constructor(private _clientsService: ClientsService) {}
ngOnInit() {
this._clientsService.getClient()
.subscribe(data => this.clients = data);
}
}
客户列表.component.html:
<app-client-detail *ngFor="let client of clients" [clients]="client" (clientSelected)="selectedClient=client"></app-client-detail>
<app-selected-client-details></app-selected-client-details>
2)客户端细节.component.ts:
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-client-detail',
templateUrl: './client-detail.component.html',
styleUrls: ['./client-detail.component.css']
})
export class ClientDetailComponent implements OnInit {
@Input() clients;
@Output() clientSelected: EventEmitter<any> = new EventEmitter;
constructor() {}
ngOnInit() {}
selectClient() {
this.clientSelected.emit();
}
}
客户端详细信息.component.html:
<div (click)="selectClient()">
<p>{{clients.name}}</p>
</div>
3) selected-client-details.component.ts:
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'app-selected-client-details',
templateUrl: './selected-client-details.component.html',
styleUrls: ['./selected-client-details.component.css']
})
export class SelectedClientDetailsComponent implements OnInit {
@Input() selectedClient;
constructor() {}
ngOnInit() {}
}
selected-client-details.component.html:
<div>
<p>{{selectedClient?.firstName}}</p>
<p>{{selectedClient?.age}}</p>
<p>{{selectedClient?.address}}</p>
</div>
如果我的 clients-list.component.html 看起来像这样 -
<app-client-detail *ngFor="let client of clients" [clients]="client" (clientSelected)="selectedClient=client"></app-client-detail>
<div>
<p>{{selectedClient?.firstName}}</p>
<p>{{selectedClient?.age}}</p>
<p>{{selectedClient?.address}}</p>
</div>
- 有用。当我点击用户时,阻止出现用户信息。但是用户的信息必须是另一个组件。还有一个问题。
我想我必须做这样的事情:
(在 clients-list.component.html 中)
<app-client-detail *ngFor="let client of clients" class="client-item" [clients]="client" (clientSelected)="selectedClient=client"></app-client-detail>
<app-selected-client-details [???]></app-selected-client-details>
但我无法理解我该怎么做。请帮我...