【问题标题】:How to generate ion-select from ts for loop如何从 ts for 循环生成离子选择
【发布时间】:2019-08-25 20:24:50
【问题描述】:

我是 Ionic 的新手。我想从 ts 文件在 html 文件中生成一个离子选择选项。在下面的代码中,我有一个 for 循环,它执行 7 次并给出 7 个不同的 this.newDate 值。我希望每次循环执行时都会在 html 文件中创建一个 ion-option。

 viewTime(){

       let url = 'http://api.timezonedb.com/v2.1/get-time-zone?key=8304HG08V&format=json&by=zone&zone=Asia/Kolkata';

        this.http.request(url, this.config.options)
        .map(res => res.json())
            .subscribe((response: any) => {
                console.log(response);
                console.log(response.timestamp);

                    var data = response.timestamp - 19800 ;
                      const datePipe = new DatePipe('en-US');
                    const time = datePipe.transform(data*1000, 'h:mm a');

                    //const datePipe = new DatePipe('en-US');
                    const serverDate = datePipe.transform(response.timestamp*1000, 'dd/MM/yyyy');
                    if(time >='16:00 PM'){

                        data = data+172800;

                        console.log("hello")
                    }

                    else{

                            data = data+86400;
                         console.log("exit")

                    }


     this.newDate = datePipe.transform(data*1000, 'dd/MM/yyyy');
        for (var i = 1; i < 8; i++) {

               var value = data+(86400*i);
               this.newDate = datePipe.transform(value*1000, 'dd/MM/yyyy');
          }

                                }, (err) => {
                                let alert = this.alertCtrl.create({
                                title: 'Error',
                                subTitle: 'Please check your credentials',
                                buttons: ['OK']
                                        });
                                 alert.present();
                                 });
                                 }
 }

如何解决?

【问题讨论】:

  • 你应该将你的 this.newDate 值推送到一个数组中,然后将其绑定到 ion-select 的 ngModel 中。
  • 感谢您的回复...您能否提供此代码。

标签: angularjs ionic3


【解决方案1】:

但是你可以使用类似下面的东西。

sample.ts

export class Sample {

  dates;
  date;

  constructor() {

    this.dates = [];
  }

  viewTime(){

  for (var i = 1; i < 8; i++) {

    var value = data+(86400*i);
    this.newDate = datePipe.transform(value*1000, 'dd/MM/yyyy');
    this.dates.push(this.newDate);

    }
  }
}

sample.html

  <ion-item>
    <ion-label>Select a Date</ion-label>
    <ion-select [(ngModel)]="date">
      <ion-option *ngFor="let date of dates"> {{date}}</ion-option>
    </ion-select>
  </ion-item>

我为你创建了一个working example。

【讨论】:

  • 它显示离子选择,但只要我点击任何离子选项,它就会给出这个 e 错误错误:尝试区分 '19/10/2018' 时出错。只允许使用数组和可迭代对象
  • 您提供的示例也仅在页面刷新时第一次工作。
  • 好的,我会解决这个问题。但我只是尝试给出一个解决方案,你问的是“如何从 ts for loop 生成离子选择”
  • @Saif khan 我更新了我的答案。看看它对你有用。
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2018-10-10
  • 2021-01-14
  • 1970-01-01
  • 2021-06-29
相关资源
最近更新 更多