您可以使用
*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;
}
然后在yout html中使用
ngfor
通过数组调用并显示多个按钮
<button ion-button *ngFor="let nmb of btnsArray" (click)="fnc(nmb)">{{nmb}}</button>
这是
NgFor docs
如果您需要更多信息。希望这有帮助。