gpt4 book ai didi

angular2-forms - 如何在 HTML View 中获取 FormControl Validator

转载 作者:行者123 更新时间:2023-12-05 07:46:14 25 4
gpt4 key购买 nike

我正在使用响应式表单。我想像这样在 View 中获取 FormControl 的验证器:

<span *ngIf="productForm.controls.ProductName.validator?.required" class="required-field">*</span>

我的组件代码是这样的:

    this.productForm = new FormGroup({
'ProductName': new FormControl('', [Validators.required])
})

是否有任何方法可以知道 ProductName 有一个验证器并且这是必需的

最佳答案

可以使用

检查
*ngIf = "productForm.controls.Productname.errors.required"

检查输入是否有必需的验证器。

关于angular2-forms - 如何在 HTML View 中获取 FormControl Validator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40969990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com