Home > Mobile >  How do I get the value from [object Object] on event change
How do I get the value from [object Object] on event change

Time:11-21

enter image description hereOn event change I'm trying to capture the value of an object that's displayed in a select drop down.

<ion-select
  placeholder="Select your itinerary"
  (ionChange)="handleChange($event)"
  (ionCancel)="pushLog('ionCancel fired')"
  (ionDismiss)="pushLog('ionDismiss fired')">
  <ion-select-option *ngFor="let itinerary of myItineraries" value="{{itinerary?.itinerary}}">{{itinerary?.itinerary.destination}}</ion-select-option>
</ion-select>

There are several properties in itinerary that I need but I'm only displaying the destination. However, I need those other properties on event change.

However, when I attempt to capture the change I'm only getting [Object object].

handleChange(e) {
  console.log('event', e.target);
  this.pushLog('ionChange fired with value: '   e.detail.value);
}

and this is the console.log:

ionChange fired with value: '[Object object]

After searching online I've tried using JSON.stringify(e.detail.value) simply gives me "[Object object]"

How do I get the actual values of the object's other properties?

CodePudding user response:

I am not familiar with Angular or Ionic...
But assuming that e.detail.value is supposed to be the selected option's value, you could try this:

<ion-select-option *ngFor="let itinerary of myItineraries" value="{{JSON.stringify(itinerary?.itinerary)}}">{{itinerary?.itinerary.destination}}</ion-select-option>

You should get it already stringified in the event handler:

this.pushLog('ionChange fired with value: '   e.detail.value);

So you could then parse it to use the object:

const eventDetails = JSON.parse(e.detail.value)
console.log(`${eventDetails.destination} was selected.`)

CodePudding user response:

this.pushLog('ionChange fired with value: ' e.detail.value); still is not true.

Try this.pushLog('ionChange fired with value;'); this.pushLog( e.detail.value);

CodePudding user response:

The error you made is skipping from here, the returned value is an object, but you are trying to concatenate it with a string. Try logging one by one

  • Related