Я написал демонстрационный проект на StackBlitz . Я использовал имя и фамилию, потому что мне было проще составлять данные. Мой код в основном состоит из трех файлов. Вот app.component.ts
import { Component, OnInit } from "@angular/core";
import { Person } from "./Person";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
name = "Angular";
people: Person[] = [
{ firstName: "john", lastName: "smith" },
{ firstName: "john", lastName: "pane" },
{ firstName: "john", lastName: "sander" },
{ firstName: "joe", lastName: "smith" },
{ firstName: "joe", lastName: "pane" },
{ firstName: "joe", lastName: "sander" },
{ firstName: "sue", lastName: "smith" },
{ firstName: "sue", lastName: "pane" },
{ firstName: "sue", lastName: "sander" },
{ firstName: "alice", lastName: "smith" },
{ firstName: "alice", lastName: "pane" },
{ firstName: "alice", lastName: "sander" },
{ firstName: "sally", lastName: "smith" },
{ firstName: "fred", lastName: "smith" },
{ firstName: "ted", lastName: "smith" },
{ firstName: "karl", lastName: "jones" },
{ firstName: "karl", lastName: "dowd" },
{ firstName: "tom", lastName: "brown" }
];
firstNameSet = new Set<string>();
lastNameSet = new Set<string>();
ngOnInit() {
this.people.forEach(person => this.firstNameSet.add(person.firstName));
this.people.forEach(person => this.lastNameSet.add(person.lastName));
}
firstChanged(event) {
let first = event.target.value;
this.lastNameSet.clear();
let firstArray = this.people.filter(person => person.firstName === first);
firstArray.forEach(person => this.lastNameSet.add(person.lastName));
}
lastChanged(event) {
let last = event.target.value;
this.firstNameSet.clear();
let lastArray = this.people.filter(person => person.lastName === last);
lastArray.forEach(person => this.firstNameSet.add(person.firstName));
}
reset(firstSelect, lastSelect) {
firstSelect.selectedIndex = 0;
lastSelect.selectedIndex = 0;
this.people.forEach(person => this.firstNameSet.add(person.firstName));
this.people.forEach(person => this.lastNameSet.add(person.lastName));
}
}
Вот app.component. html
<hello name="{{ name }}"></hello>
<form>
<select #firstSelect (change)="firstChanged($event)">
<option value="firstName">firstName</option>
<option *ngFor="let first of firstNameSet;" value="{{first}}">
{{first}}</option>
</select>
<select #lastSelect (change)="lastChanged($event)">
<option value="lastName">lastName</option>
<option *ngFor="let last of lastNameSet;" value="{{last}}">
{{last}}</option>
</select>
<button type="button" (click)="reset(firstSelect, lastSelect)">Reset</button
</form>
, а вот Person.ts
export interface Person {
firstName: string;
lastName: string;
}