gpt4 book ai didi

angular - 如何从 Ionic 动态选择选项中获取 ID 值和 NAME 值

转载 作者:行者123 更新时间:2023-12-02 20:03:15 25 4
gpt4 key购买 nike

ionic 3,我已经成功实现了动态选择选项,我希望能够将所选选项的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);

最佳答案

您的选择中不需要有两个value,只需将整个对象绑定(bind)到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)
}

<强> DEMO

关于angular - 如何从 Ionic 动态选择选项中获取 ID 值和 NAME 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55343444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com