angular - 基于布尔角反应形式有条件地应用 mat-form-field-invalid
问题描述
我正在研究一种反应形式,并且对密码字段有限制。我想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/Reactive Forms 方式进行。在组件逻辑中定义表单控件时应用它。
推荐阅读
- sql - 如何将小数点前后的值存储在 2 个不同的列中
- c# - blazor 中的 OnInitializedAsync()
- python - Python生命线包中Cox比例危险法和Weibull AFT模型的区别
- powershell - 通过windows任务调度程序将参数传递给powershell脚本不起作用
- ios - 如何从“使用 Apple 登录”验证 identityToken?
- javascript - :host 被通用选择器 css 覆盖
- google-app-engine - 我如何知道 Google App Engine 实例是标准的还是灵活的?
- linux-kernel - Binder中的双向链表task_list为什么是空的?怎么打印?
- javascript - 按首字母 JavaScript 分组的问题
- c# - 使用 Sage50 Canada 2019 的示例 sdk 代码创建销售发票时出现异常错误