简单的角度验证不起作用

时间:2015-08-04 10:14:42

标签: angularjs validation

为什么验证不起作用?这非常简单:

<div ng-app ng-controller="formCtrl" >
    <form name="productForm" ng-submit="addCartItem()" novalidate >
       <input type="text" name="quantity" ng-maxlength="2" required novalidate />
        <span class="error" ng-show="productForm.quantity.$error.required">Required!</span>
        <input id="submitProductForm" type="submit"  />
    </form>
</div>

function formCtrl($scope){
    $scope.addCartItem = function(){
        alert(productForm.$error);
    }
}

这里是jsfiddle的链接:http://jsfiddle.net/ogwsa5wn/

1 个答案:

答案 0 :(得分:4)

您忘记将ng-model添加到输入文本字段

只需使用

<input type="text" ng-model="quantity" name="quantity" ng-maxlength="2" required novalidate />

而不是

<input type="text" name="quantity" ng-maxlength="2" required novalidate />

<强> Working JSfiddle