首页 > 解决方案 > 具有 i 到 j 值的离子选择

问题描述

我想使用 ionic4 创建一个离子选择菜单,用户可以在其中选择 20 到 220 之间的值,但无需手动编写 20、21、22 等...

我已经尝试实现这篇文章Tersest way 的方法,以在 JavaScript 中从 1..20 创建一个整数数组

但我不知道如何使用打字稿将此 javascript 代码转换为我的 ionic4 应用程序...

谢谢您的帮助。

标签: angulartypescriptionic4

解决方案


What you can do is

  • First, create an array which is going to contain the numbers

    numbers = [];
    
  • Then in your ngOnInit function create a for loop to add the numbers

    ngOnInit() {
      for (let i = 20; i <= 220; i++) {
         this.numbers.push(i);
      }
    }
    
  • then use the array in your HTML file like below

    <ion-select>
        <ion-select-option *ngFor="let num of numbers">{{num}}</ion-select-option>
    </ion-select>
    

If you could share your existing codebase where you want this feature to be implemented i can show you specifically where to add the codes!


推荐阅读