[英]How can I assign a value from the Angular Material autocomplete to a variable in my component?
I'm building a form to create a POST to the API. 我正在构建一个表单来为API创建一个POST。 I'm using Angular Material 4 and I'm using the Autocomplete component provided by Material Design.
我正在使用Angular Material 4,而我正在使用Material Design提供的Autocomplete组件。
Here is my HTML Component: 这是我的HTML组件:
<mat-form-field class="example-full-width">
<input type="text" matInput placeholder="Local" [formControl]="HomeTeamCtrl" [matAutocomplete]="homeAuto">
<mat-autocomplete #homeAuto="matAutocomplete">
<mat-option *ngFor="let team of filteredHomeTeams | async" [value]="team.teamName">
{{ team.teamName }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Which is working fine, I'm able to type in and filter the results, then if I select the item from the list, It is entered in the input field and remains there. 哪个工作正常,我可以输入并过滤结果,然后如果我从列表中选择项目,它将输入到输入字段并保留在那里。
As you can see, I'm filtering the list based on a property of the object Team, which comes from an array of Team[]. 如您所见,我基于对象Team的属性过滤列表,该属性来自Team []的数组。
This object has other values of course and what I need to do is that When I select a value from my Autocomplete list of options, then it should call a method using that same object to take a string in a property, parse it and assign it to a variable. 这个对象当然还有其他值,我需要做的是当我从我的自动完成选项列表中选择一个值时,它应该使用同一个对象调用一个方法来获取属性中的字符串,解析它并分配它变量。
Here is my team class: 这是我的团队课程:
export class Team {
teamName: string;
selfLink: string;
}
This is the initial Array: 这是最初的数组:
"teams": [{
"teamName": "River";
"selfLink": "http://localhost:4200/teams/1"
},
{
"teamName": "Boca";
"selfLink": "http://localhost:4200/teams/2"
}]
I create the initial array: 我创建了初始数组:
ngOnInit(){
this.match = new Match;
this.availableTeams = [];
this.getTeams();
this.HomeTeamCtrl = new FormControl();
this.filteredHomeTeams = this.HomeTeamCtrl.valueChanges
.startWith(null)
.map(team => team ? this.filterTeams(team) : this.availableTeams.slice());
}
getTeams() {
this.teamService.getTeamsList()
.subscribe(
teams => this.availableTeams = teams,
error => console.log(error)
);
}
filterTeams(name: string) {
return this.availableTeams.filter(team =>
team.teamName.toLowerCase().indexOf(name.toLowerCase()) === 0);
}
All of this is working. 所有这些都有效。 Now as you can see, I have a "match" object that I need to complete to push it and so here comes my question.
现在你可以看到,我有一个“匹配”对象,我需要完成推送它,所以这是我的问题。
How do I proceed to do the following: 如何继续执行以下操作:
When I select a team name from my list of options in the Autocomplete, the string in the "selfLink" for that object should be parsed and assign the ID (last number) to this.match.homeTeam 当我从自动填充中的选项列表中选择团队名称时,应解析该对象的“selfLink”中的字符串, 并将ID (最后一个数字) 分配给this.match.homeTeam
The easiest way is to bind to the md-option (onSelectionChange)
and assign the local variable there. 最简单的方法是绑定到
md-option (onSelectionChange)
并在那里分配局部变量。
<mat-option *ngFor="let team of filteredHomeTeams | async" [value]="team.teamName" (onSelectionChange)="match.homeTeam = team.selfLink">
or call a function in your component 或者调用组件中的函数
**.html**
<mat-option *ngFor="let team of filteredHomeTeams | async" [value]="team.teamName" (onSelectionChange)="parseHomeTeam(team)">
**.component**
parseHomeTeam(team: Team) {
// parse team logic here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.