0

I'm developing Hybrid app using ionic 2. i am stuck in pass value from html to typescript. in this page i have first set select option from ion-select and i have set for loop(ngFor) in ion-option. below select option i have put button for add selected service to database(button is outside of ngFor so i can't send option id to typescript) and i had set option name in value.

my code is below

<ion-item>
  <ion-label>Service type</ion-label>
  <ion-select [(ngModel)]="serviceName" >
    <ion-option value="{{x.name}}" *ngFor="let x of servicelist">{{x.name}}</ion-option>
  </ion-select>
</ion-item>

<button ion-button (click)="addService()"> Add service</button>

enter image description here

10
  • can you make it clearer? like what should happen exactly, what do you want to pass? Commented Aug 30, 2018 at 12:00
  • in addService(), you could just get by this: this.serviceName.id Commented Aug 30, 2018 at 12:00
  • 1
    (click)="addService(serviceName)" ? Otherwise why would you use a [(ngModel)] ? Commented Aug 30, 2018 at 12:01
  • i want to pass x.name and x.id on click "Add service" button. Commented Aug 30, 2018 at 12:06
  • You should wrap the whole thing into a form and work with the form values when submitting using the button. Commented Aug 30, 2018 at 12:20

1 Answer 1

3

You can do following:

<ion-item>
  <ion-label>Service type</ion-label>
  <ion-select [(ngModel)]="selectedService" >
    <ion-option [value]="x" *ngFor="let x of servicelist">{{x.name}}</ion-option>
  </ion-select>
</ion-item>

<button ion-button (click)="addService(selectedService)"> Add service</button>
in button you can pass anything from selectedService object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.