AngularJS表单验证未按预期工作

AngularJS表单验证未按预期工作,angularjs,Angularjs,我在正确进行表单验证时遇到问题 我有以下表格: <form name="testForm" novalidate> <input type="text" name="name" class="form-control" id="name" ng-model="name" placeholder="Enter a name for your topic discovery" required/> <p ng-show="

我在正确进行表单验证时遇到问题

我有以下表格:

<form name="testForm" novalidate>
    <input type="text" name="name" class="form-control" id="name" ng-model="name"
                placeholder="Enter a name for your topic discovery" required/>
    <p ng-show="testForm.name.$invalid" class="help-block">A name is reqiured for the topic discovery</p>
</form>

主题发现需要一个名称


但是
元素从未显示,我做错了什么?

您需要为输入元素指定一个名称:

<input type="text" class="form-control" name="name" id="name" ng-model="name"
       placeholder="Enter a name for your topic discovery" required />


请记住,元素是按名称注册在
testForm
命名空间下的,而不是按名称注册的
ngModel
,在您的情况下,它们是相同的,但在大多数情况下是不同的。

您需要为输入元素指定一个名称:

<input type="text" class="form-control" name="name" id="name" ng-model="name"
       placeholder="Enter a name for your topic discovery" required />


请记住,元素是按名称注册在
testForm
命名空间下的,而不是按名称注册的
ngModel
,在您的情况下,它们是相同的,但在大多数情况下是不同的。

您忘了添加输入名称

<input type="text" name="name" class="form-control" id="name" ng-model="name"
                        placeholder="Enter a name for your topic discovery" required/>

完整示例:

<form name="testForm" novalidate>
        <input type="text" name="name" class="form-control" id="name" ng-model="name"
                    placeholder="Enter a name for your topic discovery" required/>
        <p ng-show="testForm.name.$touched && testForm.name.$invalid" class="help-block">A name is reqiured for the topic discovery</p>

主题发现需要一个名称


您忘记添加输入名称了

<input type="text" name="name" class="form-control" id="name" ng-model="name"
                        placeholder="Enter a name for your topic discovery" required/>

完整示例:

<form name="testForm" novalidate>
        <input type="text" name="name" class="form-control" id="name" ng-model="name"
                    placeholder="Enter a name for your topic discovery" required/>
        <p ng-show="testForm.name.$touched && testForm.name.$invalid" class="help-block">A name is reqiured for the topic discovery</p>

主题发现需要一个名称


问题在于您的type属性值是“text”,这很难变得无效。例如,将其更改为“email”,然后将您的输入元素的name属性的值设置为某个“myName”(用于视觉差异)(如上所述)。您还可以向
元素的ng show属性添加另一个条件,如下所示:

<p ng-show="testForm.myName.$invalid && testForm.myName.$dirty" class="help-block">A name is reqiured for the topic discovery</p>
主题发现需要一个名称


问题在于您的type属性值是“text”,这很难变得无效。例如,将其更改为“email”,然后将您的输入元素的name属性的值设置为某个“myName”(用于视觉差异)(如上所述)。您还可以向
元素的ng show属性添加另一个条件,如下所示:

<p ng-show="testForm.myName.$invalid && testForm.myName.$dirty" class="help-block">A name is reqiured for the topic discovery</p>
主题发现需要一个名称


当文本框为空时,它具有类
表单控件ng dirty ng invalid ng invalid required
当文本框为空时,它具有类
表单控件ng dirty ng invalid ng invalid required
仔细检查所有内容,这是您的错误,它应该可以工作。仔细检查每件事,这是你的错误,它应该可以工作。