'How can I get element value by Id in Angular
I have to get value of element by Id, I need string value and by selecting state from dropdown list, API returning ID. Now, what I've to do? My sample code:
ShippingDetails.html
<ion-item lines="none" style="--border-radius: 5px;">
<ion-label>State</ion-label>
<ion-select value="0" okText="OK" #S (ionChange)="onStateChange(S.value)" cancelText="Cancel"
[(ngModel)]="address.state" required interface="popover">
<ion-select-option value="0">Select State</ion-select-option>
<ion-select-option *ngFor="let item of deliveryStateList" value="{{item.statE_ID}}">{{ item.statE_NAME}}
</ion-select-option>
</ion-select>
</ion-item>
ShippingDetails.ts
async getDeliveryStateList() {
const payload = {
countryId: 1
};
this.generalService.getStateList(payload).subscribe(async res => {
let response = new ApiResponse();
response = Object.assign(response, res);
this.deliveryStateList = response.data;
});
}
async getDeliveryCityListList(stateid) {
const payload = {
// tslint:disable-next-line:radix
stateId: parseInt(stateid)
};
this.generalService.GetCityListbyState(payload).subscribe(async res => {
let response = new ApiResponse();
response = Object.assign(response, res);
this.deliveryCityList = response.data;
});
}
onStateChange(stateid) {
this.getDeliveryCityListList(stateid);
}
Solution 1:[1]
You're not using ionChange right. Try with:
(ionChange)="onStateChange($event)
And in ts change your method to:
onStateChange(event) {
this.getDeliveryCityListList(event.detail.value);
}
If you want to make sure the getDeliveryCityListList will get called with a string and not a number, you can do:
this.getDeliveryCityListList(event.detail.value.toString());
or:
this.getDeliveryCityListList(event.detail.value + '');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Misha Mashina |
