theCoder
theCoder

Reputation: 751

Unable to add multiple objects in array using ionic

I am trying to store multiple objects in array but I was able to store only one object at a time. I want to add multiple dates selected by the user and save them in an array but whenever I add the second date it replaces the first one.
My HTML code is:

  <div class="appointmensts">
     <ion-label>Appointmensts</ion-label>

     <ion-row>
       <ion-col col-8>
          <ion-item>
              <ion-datetime placeholder="dd-mm-yyyy" displayFormat="YYYY-MM-DD"   [(ngModel)]="addServices.appointment_dates" ></ion-datetime>
            </ion-item>
       </ion-col>
       <ion-col><span><button (click)="addService()"><ion-icon><img src="../../../assets/img/addservice.png" alt=""></ion-icon>Add</button></span></ion-col>
     </ion-row>
   </div>

My .ts code is:

public addServices: {  service_name: string , price: string, duration : string ,unit : string,  appointment_dates : Array<AnalyserNode>} = {


    service_name : '',
    price : '',
    duration : '',
    unit : '',
    appointment_dates : []


  }



  addService(){



  console.log(this.addServices.appointment_dates);

  }

Upvotes: 1

Views: 404

Answers (1)

ppichier
ppichier

Reputation: 1125

Maybe you can bind your [(ngModel)] with a new variable for example appointment_tmp;

and then in your addService()

this.addServices.appointment_dates.push(this.appointment_tmp);

working example

Upvotes: 3

Related Questions