Html 动态创建具有特定功能值的按钮数

Html 动态创建具有特定功能值的按钮数,html,typescript,ionic2,components,innerhtml,Html,Typescript,Ionic2,Components,Innerhtml,如何在类型脚本Ionic2应用程序中实现: 如果我有btnNmb:number;例如,使用当前值9,然后在home.html表单上动态创建9个按钮,并将此特定范围9中的每个数字附加到button1值=1、button2值=2等生成的每个按钮上。,要在函数fncvalue中传递此值并通过单击此fncvalue{this.x=value;}中的“从动态创建”按钮将其分配给此.x变量,可以使用*ngFor来实现此目的,但ngFor需要一个数组或任何具有iterable接口的对象,因此可以执行以下操作:

如何在类型脚本Ionic2应用程序中实现:

如果我有btnNmb:number;例如,使用当前值9,然后在home.html表单上动态创建9个按钮,并将此特定范围9中的每个数字附加到button1值=1、button2值=2等生成的每个按钮上。,要在函数fncvalue中传递此值并通过单击此fncvalue{this.x=value;}中的“从动态创建”按钮将其分配给此.x变量,可以使用*ngFor来实现此目的,但ngFor需要一个数组或任何具有iterable接口的对象,因此可以执行以下操作:

在TypeScript文件中:

export class YourClass {
  public x = number;
  public btnNmb: number = 9;
  public btnsArray: Array<number> = []; // You need to initialize it so your template doesn't break

  constructor () {
    // You can call it here or on ionViewDidLoad() lifecycle hook
    // Or if the btnNmb is a dynamic number you can call it again when the value changes
    this.generateNumberArray();
  }

  // You need to iterate from 1 to the number given in the btnNmb property
  generateNumberArray = () => {
    for(var i = 1; i <= this.btnNmb; i++){
      this.btnsArray.push(i);
    }
  }

  // The function that's triggered by button click
  fnc = number => this.x = number;
}

如果您需要更多信息,请参阅下面的示例。希望这有帮助。

您好,很抱歉回答晚了,很有用的例子,谢谢
<button ion-button *ngFor="let nmb of btnsArray" (click)="fnc(nmb)">{{nmb}}</button>