根据Angular 4中复选框的更改,对输入运行验证

时间:2017-11-14 15:34:46

标签: javascript html angular angular-forms angular-validation

我循环遍历一组键值,以创建一个复选框列表,每个复选框都有一个兄弟禁用输入。检查每个复选框后,兄弟输入文本字段将变为启用状态并且是必需的。在此视图中,有一个“上一个”和“下一个”按钮,如果用户选中一个复选框,则不应该禁用“下一个”按钮,然后不在其中输入所需的兄弟输入。我几乎有这个工作,但是一旦用户选中该框,“下一个”按钮就会被禁用,因为这意味着他们没有在所需的文本输入中输入任何内容。现在,只有当用户选中复选框时,“下一个”按钮才会被禁用,专注于兄弟输入,然后在不输入的情况下离开。

我的HTML ...

<div *ngFor="let promotion of promotionOptions; let i = index">
    <div class="col-md-6 input-container radio-label">
        <mat-checkbox [checked]="!promotion.key" (change)="promotion.key = !promotion.key">
            {{ promotion.name }}
        </mat-checkbox>
    </div>
    <mat-input-container>
        <input matInput [disabled]="promotion.key" placeholder="Cost" name="promotionCost{{i}}" #promotionCost="ngModel" [ngModel]="" (keyup)="promotionCostInput($event.target.value)"
            [required]="!promotion.key" type="number">
        <div *ngIf="promotionCost.errors && (promotionCost.dirty || promotionCost.touched)" class="alert alert-danger cost-alert">
            <div [hidden]="!promotionCost.errors.required">Please enter the checked promotion's cost</div>
        </div>
    </mat-input-container>
</div>

<div class="clearfix"></div>

<div class="button-container">
    <button class="main-btn dark icon-left" (click)="updateStep(1)"><i class="fa fa-angle-left"></i>Previous</button>
    <button class="main-btn icon-right" (click)="updateStep(3)" [disabled]="!promotionCostValid">Next<i class="fa fa-angle-right"></i></button>
</div>

我在.ts文件中使用的方法用于禁用“下一步”按钮:

promotionCostInput(value) {
    if (!value) {
      this.promotionCostValid = false;
    } else {
      this.promotionCostValid = true;
    }
  }

当用户选中复选框时,如何验证兄弟输入?

1 个答案:

答案 0 :(得分:1)

您的问题是next按钮的状态仅在您的任何输入触发keyup事件时更新。此外,它仅使用一个输入的值进行更新,但根据您的说法,您需要检查ngFor的每个输入是否已填充。

我建议您在模型中存储输入值,并在检查输入更改或选中复选框时检查促销费用是否适用于所有促销活动。

<div *ngFor="let promotion of promotionOptions; let i = index">
  <div class="col-md-6 input-container radio-label">
    <mat-checkbox [checked]="!promotion.key" (change)="promotion.key = !promotion.key; checkPromotionCost();">
      {{ promotion.name }}
    </mat-checkbox>
  </div>
  <mat-input-container>
    <input
      matInput
      [disabled]="promotion.key"
      placeholder="Cost"
      name="promotionCost{{i}}"
      (keyup)="promotion.cost = $event.target.value; checkPromotionCost();"
      [required]="!promotion.key" type="number"
    >
    <div *ngIf="promotionCost.errors && (promotionCost.dirty || promotionCost.touched)" class="alert alert-danger cost-alert">
      <div [hidden]="!promotionCost.errors.required">
        Please enter the checked promotion's cost</div>
      </div>
  </mat-input-container>
</div>

<div class="clearfix"></div>

<div class="button-container">
  <button class="main-btn dark icon-left" (click)="updateStep(1)"><i class="fa fa-angle-left"></i>Previous</button>
  <button class="main-btn icon-right" (click)="updateStep(3)" [disabled]="!promotionCostValid">Next<i class="fa fa-angle-right"></i></button>
</div>

在控制器中:

checkPromotionCost() {
  this.promotionCostValid = true;
  this.promotionOptions.forEach(promotion => {
    if (promotion.key && promotion.cost === '') {
      this.promotionCostValid = false;
    }
  });
}