【问题标题】:Apply mat-form-field-invalid conditionally based on boolean Angular Reactive Form基于布尔角反应形式有条件地应用 mat-form-field-invalid
【发布时间】:2021-06-09 22:43:18
【问题描述】:

我正在处理一个反应式表单,我对密码字段有限制。我想根据布尔值手动应用类'mat-form-field-invalid'。

我的 HTML

<mat-form-field
          appearance="outline"
          [ngClass]="{
            'mat-form-field-invalid':
              !passwordsRequirementSatisfied && f.password.touched
          }"
        >
          <mat-label>Password</mat-label>
          <input
            matInput
            [type]="hidePassword ? 'password' : 'text'"
            (keyup)="validatePassword()"
            formControlName="password"
            required
          />
          <button
            mat-icon-button
            matSuffix
            (click)="hidePassword = !hidePassword"
            type="button"
          >
            <mat-icon>{{
              hidePassword ? "visibility_off" : "visibility"
            }}</mat-icon>
          </button>
</mat-form-field>

非常感谢任何帮助

【问题讨论】:

    标签: angular sass angular-material


    【解决方案1】:

    您可以通过编写 custom validator 以 Angular/Reactive Forms 的方式进行更改,而不是手动更改类以影响样式。在组件逻辑中定义表单控件时应用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 2020-01-16
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2019-05-24
      • 2021-12-06
      相关资源
      最近更新 更多