离子3-如果同时激活所有(JS)

时间:2018-06-30 22:45:04

标签: javascript function if-statement button ionic-framework

我有一个小问题,我找不到解决方法,

我在这里有这个小功能:

Clicked(){
       for (let i = 0; i < 5; i++) {
          if (this.ShareProvider.ValueFonction(i)) {
            alert(i)
          }
        }
}

有6个按钮,我要执行的操作是单击按钮alert()时将启动。问题是,当我运行该程序时,它连续显示6个警报。 我试图用不同的方式写但又有同样的问题。

if (this.ShareProvider.ValueFonction(1)) {
  alert(1)
}
if (this.ShareProvider.ValueFonction(2)) {
  alert(2)
}
if (this.ShareProvider.ValueFonction(3)) {
  alert(3)
}
if (this.ShareProvider.ValueFonction(4)) {
  alert(4)
}
if (this.ShareProvider.ValueFonction(5)) {
  alert(5)
}
if (this.ShareProvider.ValueFonction(6)) {
  alert(6)
}

if (this.ShareProvider.ValueFonction(1)) {
  alert(1)
}
if (this.ShareProvider.ValueFonction(2)) {
  alert(2)
}
if (this.ShareProvider.ValueFonction(3)) {
  alert(3)
}
if (this.ShareProvider.ValueFonction(4)) {
  alert(4)
}
if (this.ShareProvider.ValueFonction(5)) {
  alert(5)
}
if (this.ShareProvider.ValueFonction(6)) {
  alert(6)
}

感谢您的关注

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式执行此操作,我建议您在Ts模板上创建一个数组,然后在HTML的ngFor中调用它们,并带有(单击)函数以当前索引调用alert。例如:

在TS模板中:

声明一个数组var,其长度在类的顶部,例如:

array= new Array(5);

提醒索引的功能

clicked(i){
     alert(i);
}

并在html模板中:

  <div *ngFor="let a of array; let i = index">
    <button (click)="alert(i)> Click me </button>
  </div>

那将显示基于元素索引的警报