Html 如果要检查输入字段是否为空,则ng出现问题

Html 如果要检查输入字段是否为空,则ng出现问题,html,angular-ng-if,ng-show,Html,Angular Ng If,Ng Show,我有一个输入标签 <form name="patientSearchForm"> <input name="text" id="textType" type="text" class="form-control" /> <button type=&quo

我有一个输入标签

<form name="patientSearchForm">

<input name="text" id="textType" type="text" class="form-control" /> 
                                                       
<button type="submit">Submit Form</button><br /><br />
                    
</form>                   

提交表格

如何使用ng if显示输入字段不能为空的消息。 我正在使用

<span style="color:Red" ng-if="!textType.value" ng-show="patientSearchForm.$submitted"> My message </span> 
我的留言

但它不起作用。请帮忙。

试试这个


我的留言

您使用的角度形式有点不同

首先,您需要在component.ts文件中创建一个表单,然后向其中添加一个formcontrol。在HTML中使用它,然后您可以检查错误

// In component.ts file, 

someForm = new FormGroup({
 someFormControl: new FormControl('')
});

// In HTML file 

<div [formGroup]="someForm">
  <input 
      name="someFormControl" 
      formControlName="someFormControl"
      placeholder="Sample Input text"
   />

   // Use ngIf directive here to check the value

  <span *ngIf="!someForm.controls.someFormControl.value"> 
      My error message 
  </span>

// The exclamation point will check if there is a value or not. if a value is there, that error message wont be shown; but if there is no value in the input field, this error will be shown
</div>


//在component.ts文件中,
someForm=新表单组({
someFormControl:新FormControl(“”)
});
//在HTML文件中
//在此处使用ngIf指令检查值
我的错误消息
//感叹号将检查是否有值。如果存在值,则不会显示该错误消息;但如果输入字段中没有值,则会显示此错误

不,它不工作。感谢您的帮助如果您不想根据需要验证元素,可以使用本教程: