当前位置: 首页 > 知识库问答 >
问题:

带if条件和布尔值的循环的类型脚本

阚小云
2023-03-14

我有一个for循环,它生成了2个按钮,这两个按钮都根据每行的情况显示或隐藏。出于某种原因,我为它使用了一个布尔值,当满足一个条件时,所有的行都会受到影响,但我只希望那个特定的行受到影响。以下是我的代码。这个程序是用爱奥尼亚角写的。

前端HTML文件

<ion-list *ngFor="let ml of miqaatITS">
  <ion-item>
    <ion-label><b>{{ml.event}}</b><br>{{ml.date}}<br>{{ml.time}}</ion-label>
    <ion-button color="dark" [routerLink]="['/home/Allocation']" *ngIf="isPass">VIEW PASS</ion-button>
    <ion-button color="dark" fill="outline" *ngIf="!isPass" disabled>NO PASS</ion-button>
    <ion-button color="danger" >CANCEL PASS</ion-button>
  </ion-item>
</ion-list>

.ts文件

 isPass = false;
feedData() {
console.log(this.authUser);
this.postData.user_id = this.authUser.user_id;
this.postData.token = this.authUser.token;
this.postData.itsnumber = this.authUser.itsnumber;
if (this.postData.user_id && this.postData.token) {
  this.miqaat.miqaatITSData(this.postData).subscribe(
    (res: any) => {
      console.log(res);
      for(var pass of res.miqaatITS){
        console.log(pass.allocation == 1);
        if(pass.allocation == 1) {
          this.isPass = !this.isPass;
        }
      }
      this.miqaat.changeMiqaatData(res.miqaatITS);
    },
    (error: any) => {
      this.toastService.presentToast('Network Issue.');
    }
  );
}
}

请指教

共有2个答案

柴飞星
2023-03-14

你可以用这种方式,

Html代码。

<ion-list *ngFor="let ml of miqaatITS">
  <ion-item>
    <ion-label><b>{{ml.event}}</b><br>{{ml.date}}<br>{{ml.time}}</ion-label>
    <ion-button color="dark" [routerLink]="['/home/Allocation']" *ngIf="ml.allocation">VIEW PASS</ion-button>
    <ion-button color="dark" fill="outline" *ngIf="!ml.allocation" disabled>NO PASS</ion-button>
    <ion-button color="danger" >CANCEL PASS</ion-button>
  </ion-item>
</ion-list>

.ts 代码

miqaatITS:any;
 isPass = false;
feedData() {
console.log(this.authUser);
this.postData.user_id = this.authUser.user_id;
this.postData.token = this.authUser.token;
this.postData.itsnumber = this.authUser.itsnumber;
if (this.postData.user_id && this.postData.token) {
  this.miqaat.miqaatITSData(this.postData).subscribe(
    (res: any) => {
      console.log(res);
      this.miqaatITS = res.miqaatITS
      
      this.miqaat.changeMiqaatData(res.miqaatITS); // Dont know why you have used this. Remove if this is it not usefull.
    },
    (error: any) => {
      this.toastService.presentToast('Network Issue.');
    }
  );
}
}
西门展
2023-03-14

在这种情况下,<code>isPass</code>是一个布尔值。它将获取for循环中的最后一个值。您需要做的是,为每一行引入一个新变量,然后应用它。您将获得所有行的is pass,而不是单个ispass。大致如下:

************  In .ts code ********
console.log(res);
      for(var pass of res.miqaatITS){
        let pass['isPass'] = false;
        console.log(pass.allocation == 1);
        if(pass.allocation == 1) {
          pass['isPass'] = !pass['isPass'];
        }

********* in html ********
<ion-button color="dark" fill="outline" *ngIf="!!ml.isPass" disabled>NO PASS</ion-button>

Note: you need two exclamation marks, because it'll check for null value too. 
 类似资料:
  • 这将使约束在范围内,为提供额外的参数。这里我的意图是包含一些(隐藏的)具体类型,它应该用作多态函数GHC compulins的具体类型: 我的用例的假设是1。同时计算和2。隐藏类型的值涉及(至少部分)第一个元组元素的计算。这意味着我不想在中调用两次(一次是获取,一次是使用该类型绑定第一个元组元素)。在存在约束的情况下,是否有某种方法使的定义成为可能?

  • 今天我得到了一个关于代码的评论,考虑到我在学校作业中检查变量是真还是假的方式。 我编写的代码如下所示: 他们说这样写更好/更整洁: 例如,0和1不应该在该变量中。

  • 本文向大家介绍Shell脚本的条件控制和循环语句,包括了Shell脚本的条件控制和循环语句的使用技巧和注意事项,需要的朋友参考一下 条件判断:if语句 语法格式: 注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句: 示例: if ... else 语句也可以

  • 我不知道为什么人工智能不能返回真假。它打印“丢失的返回声明”我在网上寻找答案,但没有一个能解决我的问题。

  • 对Java来说很新鲜,我在大学的一个入门班做一个项目。我正在尝试做一个方法,在String数组中搜索输入的状态并返回索引。如果用户输入不在数组中的查询,我希望它要求一个新的状态来搜索。我的例外是说“变量statePotion可能尚未初始化。”下面是代码。 提前谢谢!

  • 问题内容: 哪种是更好的Java编码风格? 要么: 问题答案: 当然是前者。后一种说法是多余的,只是表明您对布尔值的概念不太了解。 另一个建议:为变量选择其他名称。按照此Java样式指南: is前缀应用于布尔变量和方法。 ,,, , 这是Sun用于Java核心软件包的方法和变量的命名约定 。 使用前缀可以解决选择错误的布尔名称(如或)的常见问题。甚至 根本不合适,程序员被迫选择更有意义的名称。 变