0

我的 AngularJS 验证不起作用。我添加了 ng-required。请让我知道我在哪里失踪

<ng-form id="frmdisbursementScheduleMaintenance">
            <div style="padding-bottom: 8px;">
                <button id="cm-SaveBtn" name="cm-SaveBtn" type="button" ng-click="submitted=true">Save</button>
                <button id="cm-RefreshBtn" name="cm-RefreshBtn" type="button">Refresh</button>
            </div>
          <div>
                    <table>

                        <tr>
                            <td><span class="VNAVLabel">Process Begin Date: </span></td>
                            <td> 
                                <span><input type="date" name="processBeginDate" ng-model="processBeginDate" required></span>
                                <span ng-show="(frmdisbursementScheduleMaintenance.processBeginDate.$dirty || submitted) && frmdisbursementScheduleMaintenance.processBeginDate.$error.required">
                                    Process Begin Date is required
                                </span>
                            </td>
                        </tr>

                    </table>
                </div>

</ng-form>
4

2 回答 2

2

它不应该是id,你应该使用name属性来代替。Angular 将创建$scope带有名称的变量,frmdisbursementScheduleMaintenance其中包含表单信息。

<ng-form name="frmdisbursementScheduleMaintenance">
于 2017-03-21T17:33:30.930 回答
2

对表单元素使用“name”而不是“id”。

此外,您不必(frmdisbursementScheduleMaintenance.processBeginDate.$dirty || submitted)只检查frmdisbursementScheduleMaintenance.processBeginDate.$error.required就可以完成这项工作。

<ng-form name="frmdisbursementScheduleMaintenance">
   <div style="padding-bottom: 8px;">
      <button id="cm-SaveBtn" name="cm-SaveBtn" type="button" ng-click="submitted=true">Save</button>
      <button id="cm-RefreshBtn" name="cm-RefreshBtn" type="button">Refresh</button>
   </div>
   <div>
      <table>
         <tr>
            <td><span class="VNAVLabel">Process Begin Date: </span></td>
            <td> 
               <span><input type="date" name="processBeginDate" ng-model="processBeginDate" required></span>
               <span ng-show="frmdisbursementScheduleMaintenance.processBeginDate.$error.required">
               Process Begin Date is required
               </span>
            </td>
         </tr>
      </table>
   </div>
</ng-form>
于 2017-03-21T17:44:29.277 回答