2014-09-23 75 views
0

我需要確保name輸入與模式匹配。 AngularJS無法看到name並設置了所需的錯誤,儘管它們駐留在一個div中。從form開始聲明是不合理的,因爲它有很多沒有名字的div。如何讓required字段對Angular可見?AngularJS無法看到必填字段

<form ng-app="" ng-controller="myCtrl" action="" name="productForm" class="form-horizontal" id="addproductForm" role="form" novalidate> 
    <div class="form-group" ng-class="{ 'has-error': productName.$invalid }"> 
     <label for="product_name" class="col-sm-3 control-label">product name: </label> 
     <div class="col-sm-8"> 
      <input id="product_name" name="productName" type="text" ng-model="testproductName" ng-pattern='/^[a-zA-Z0-9_.-]*$/' required/> 
     </div> 
    </div> 
</form> 

回答

3

在輸入屬性中需要ng-model才能使角度觀察它。 並且您的錯誤需要附加到您的表單名稱。

看到這個:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app> 
 

 
    <form name="form"> 
 
    <input ng-model="test" name="myField" required> 
 
    <span ng-show="form.myField.$error.required">Required</span> 
 
    </form> 
 
    
 
</div>