【问题标题】:Unable to add multiple objects in array using ionic无法使用离子在数组中添加多个对象
【发布时间】:2019-06-30 04:26:36
【问题描述】:

我试图将多个对象存储在数组中,但我一次只能存储一个对象。我想添加用户选择的多个日期并将它们保存在一个数组中,但是每当我添加第二个日期时,它都会替换第一个日期。
我的 HTML 代码是:

  <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>

我的 .ts 代码是:

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);

  }

【问题讨论】:

    标签: angular typescript ionic-framework ionic2 ionic3


    【解决方案1】:

    也许您可以将您的 [(ngModel)] 绑定到一个新变量,例如 appointment_tmp;

    然后在你的 addService() 中

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

    working example

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多