【发布时间】:2018-10-26 17:47:38
【问题描述】:
我已经使用 Angular Material 创建了一个自动完成字段,并成功从 web api 获取国家/地区列表。
CountryID -> 项目值(或索引)
国家 -> 项目文本
当我尝试获取所选项目的值(不是文本)时,它会按预期返回文本。但我需要获取所选项目的价值。
这是我的代码:
this.WeatherSearchForm.get('country').value; // this returns the selected country name, ex: France
和
<md-input-container>
<input mdInput placeholder="Select Country..." [mdAutocomplete]="auto" class="form-control validate filter-input" formControlName="country">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete" md-input-name="autocompleteField" required md-input-minlength="2" md-input-maxlength="18"
md-select-on-match required md-input-minlength="2">
<md-option *ngFor="let country of countries | async" [value]="country.Country">
{{ country.Country }}
</md-option>
</md-autocomplete>
编辑: 在我改变这一行之后
<md-option *ngFor="let country of countries | async" [value]="country.Country">
到这里,
<md-option *ngFor="let country of countries | async" [value]="country.CountryID">
效果很好,this.WeatherSearchForm.get('country').value; 返回了 CountryID。
但在自动完成字段中选择国家后,在 UI 端,现在我看到 CountryID 不是 Country。
【问题讨论】:
-
请添加现在显示ID的UI元素的代码(右边6的那个)。
-
其实这是显示6的代码,
标签: angular autocomplete angular-material2