如何从 Ionic Dynamic Select 选项中获取 ID 值和 NAME 值
How to get both the ID value and the NAME value from Ionic Dynamic Select Options
ionic 3,我已经成功实现了动态select选项,我希望能够将selected to Option的ID值和Name值发送到服务器
因为我只能得到ID或名字,我不能得到两个值,请帮助,我试过JSON.parse,id.value和name.value, 在选项中,但遗憾的是我只能获得第一个值而不能同时获得两者
<ion-list >
<ion-item class="list-but">
<ion-label>Tipo de Ambiente</ion-label>
<ion-select
[(ngModel)]="idType"
multiple="false"
cancelText="Cancelar"
okText="Ok"
>
<ion-option
*ngFor="let type of type_environment"
value="{{ type.name }}"
value="{{ type.id }}"
#name
>{{ type.name }}</ion-option
>
</ion-select>
</ion-item>
</ion-list>
<button ion-button block (click)="insert()">Salvar</button>
``public type_environment = new Array<any>();
public idType: number = 0;
public nameType: string = "name";
private userData: any;`
```
insert() {
this.userData = this.user.getUserData();
let body = {
name: this.nameType,
id_type: this.idType,
id_unity: this.navParams.get("id.unidade")
};
console.log("name:", this.idType);
this.route.postData("/ambiente", body).subscribe(
data => {
let response = data as any;
let ret = JSON.parse(response._body);
if (ret.insertId) {
this.navCtrl.popToRoot();
} else {
console.log(ret);
}
},
error => {
console.log(error);
您的 select 中不需要有两个 value
,只需将整个对象绑定到 value
,即:
<ion-select [(ngModel)]="idType" cancelText="Cancelar" okText="Ok">
<ion-option *ngFor="let type of type_environment" [value]="type">
{{ type.name }}
</ion-option>
</ion-select>
在此之后,您的对象值在 idType
:
insert() {
// below is your chosen object and its properties
console.log(this.idType.id, this.idType.name)
}
ionic 3,我已经成功实现了动态select选项,我希望能够将selected to Option的ID值和Name值发送到服务器
因为我只能得到ID或名字,我不能得到两个值,请帮助,我试过JSON.parse,id.value和name.value, 在选项中,但遗憾的是我只能获得第一个值而不能同时获得两者
<ion-list >
<ion-item class="list-but">
<ion-label>Tipo de Ambiente</ion-label>
<ion-select
[(ngModel)]="idType"
multiple="false"
cancelText="Cancelar"
okText="Ok"
>
<ion-option
*ngFor="let type of type_environment"
value="{{ type.name }}"
value="{{ type.id }}"
#name
>{{ type.name }}</ion-option
>
</ion-select>
</ion-item>
</ion-list>
<button ion-button block (click)="insert()">Salvar</button>
``public type_environment = new Array<any>();
public idType: number = 0;
public nameType: string = "name";
private userData: any;`
```
insert() {
this.userData = this.user.getUserData();
let body = {
name: this.nameType,
id_type: this.idType,
id_unity: this.navParams.get("id.unidade")
};
console.log("name:", this.idType);
this.route.postData("/ambiente", body).subscribe(
data => {
let response = data as any;
let ret = JSON.parse(response._body);
if (ret.insertId) {
this.navCtrl.popToRoot();
} else {
console.log(ret);
}
},
error => {
console.log(error);
您的 select 中不需要有两个 value
,只需将整个对象绑定到 value
,即:
<ion-select [(ngModel)]="idType" cancelText="Cancelar" okText="Ok">
<ion-option *ngFor="let type of type_environment" [value]="type">
{{ type.name }}
</ion-option>
</ion-select>
在此之后,您的对象值在 idType
:
insert() {
// below is your chosen object and its properties
console.log(this.idType.id, this.idType.name)
}