如果单击按钮,则仅需要设置角度 [英] angular only set required if button is clicked

查看:31
本文介绍了如果单击按钮,则仅需要设置角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果仅单击按钮,如何以角度设置必填字段.我目前的错误是在用户没有输入或点击按钮的情况下已经显示了错误.

How to set required field in angular if only button is clicked. My current one is that the error is already showing without the user typing or clicking the button.

我想要的是红色错误需要文件夹名称";只有当用户单击提交按钮并且该字段为空时才会弹出.有什么想法吗?谢谢.

What I want is that the red error "folder name is required" should only pop-up if when the user click the submit button and the field is empty. Any idea guys ? Thanks.

private _createModelForm(): FormGroup {
    return this.formBuilder.group({
      propertyId: this.data.propertyId,
      folderName: new FormControl('', [
        Validators.required,
        Validators.pattern(/[a-zA-Z0-9\.\-]+.?/)
      ]),
      documentManagementFolderId:this.data.documentManagementFolderId
    });
  }




<mat-error *ngIf="modelForm.get('folderName').hasError('pattern')">
                    Folder name must not include special characters.
                </mat-error>

推荐答案

你也需要检查 dirtytouched 属性

You need to check for dirty and touched properties too

类似:

<mat-error *ngIf="(modelForm.get('folderName').dirty || modelForm.get('folderName').touched) && modelForm.get('folderName').hasError('pattern')">
                    Folder name must not include special characters.
                </mat-error>

这篇关于如果单击按钮,则仅需要设置角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆