如何在angular 6中获取选择属性

如何在angular 6中获取选择属性,angular,select,attributes,angular6,option,Angular,Select,Attributes,Angular6,Option,我尝试了下面的代码来获取国家的数据属性值,但没有成功 fieldId:target.getAttribute('data-country\u id'), const selectEl=event.target; 变量id=角度元素(事件)数据(“国家/地区id”); console.log(“test”+event.currentTarget.getAttribute(“数据id”) {{state.name} 您可以使用[value]绑定ngModel来实现这一点 <select

我尝试了下面的代码来获取国家的数据属性值,但没有成功

fieldId:target.getAttribute('data-country\u id'),
const selectEl=event.target;
变量id=角度元素(事件)数据(“国家/地区id”);
console.log(“test”+event.currentTarget.getAttribute(“数据id”)

{{state.name}

您可以使用
[value]
绑定
ngModel
来实现这一点

 <select name="state"  [(ngModel)]="model.state"  id="state"  (ngModelChange)="GetCitySelected($event)" >
      <option *ngFor="let state of StateList" [value]="state" [attr.data-country_id]="state.country_id">{{state.name}}</option>
 </select>

{{state.name}

在这里,您需要将
value
属性的数据与
ngModel
变量相匹配。

您可以使用
[value]
绑定
ngModel
来实现这一点

 <select name="state"  [(ngModel)]="model.state"  id="state"  (ngModelChange)="GetCitySelected($event)" >
      <option *ngFor="let state of StateList" [value]="state" [attr.data-country_id]="state.country_id">{{state.name}}</option>
 </select>

{{state.name}

在这里,您需要将
属性的数据与
ngModel
变量匹配。

您需要选择选项索引来获取所选选项的属性值

Html

<select name="state" [(ngModel)]="model.state" (change)="stateChange($event)"  id="state">
  <option *ngFor="let state of StateList" [value]="state.id" [attr.data-country_id]="state.country_id">{{state.name}}</option>
</select>

对于您的解决方案,我使用(更改)事件而不是(ngModelChange)。

您需要所选选项索引来获取所选选项的属性值

Html

<select name="state" [(ngModel)]="model.state" (change)="stateChange($event)"  id="state">
  <option *ngFor="let state of StateList" [value]="state.id" [attr.data-country_id]="state.country_id">{{state.name}}</option>
</select>

对于您的解决方案,我使用了(更改)事件而不是(ngModelChange)。

如果需要进一步帮助,请与我联系:)如果需要进一步帮助,请ping我:)为什么不在函数中传递值
(ngModelChange)=“GetCitySelected($event,state.country\u id)”
另一个想法是
countryId=StateList.find(x=>x.id==model.state)。country\u id
为什么不在函数中传递值
(ngModelChange)=“GetCitySelected($event,state.country\u id)”
另一个想法是
countryId=StateList.find(x=>x.id==model.state)。country\u id