简体   繁体   English

如何将Angular Material自动完成中的值分配给组件中的变量?

[英]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.

相关问题 如何在Angular 2 Material Autocomplete中获取所选项目值 - How can I get selected item value in Angular 2 Material Autocomplete 如何在组件的角度2材质上设置数据选择器输入? - how can I set a datapicker input on angular 2 material from a component? 如何获取 I18n 变量值我可以返回到我的 Angular 父组件? - How to get a I18n variable value I can return to my Angular parent component? 如何从 Angular 中的一个组件到另一个组件获取任何字符串变量的值 - How can I get the value of any String variable from one component to another component in Angular 如何从 angular 的构造函数中为变量赋值? - How I assign a value to variable from constructor in angular? 如何在角度2中从模块到我的组件访问变量的值? - How to access the value of a variable from a module to my component in angular 2? 通过单击角度2从* ngFor中的另一个组件分配变量值 - Assign variable value from another component in *ngFor by click in angular 2 如何通过从数据库(角度6和firebase)中检索数据的服务为组件变量赋值? - How to assign value to a component variable from a service retrieving data from a database (angular 6 and firebase)? 如何从组件中的方法创建和分配任意Angular2本地模板变量 - How to create and assign an arbitrary Angular2 local template variable from a method in my component Angular:如何将所选单选按钮的值分配给.ts组件中的变量? - Angular: How to assign the value of the selected radio button to a variable in the .ts component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM