我正在使用Angularjs的ngMessages验证我的表单,当ng-form
的名称是诸如ng-form="form.formAddRoster"
之类的对象类型时,就会出现问题,这会导致ng-messages无法正常工作。我还尝试了form.formAddRoster
和formAddRoster``` inside
ng-messages but the problem still not resolved, with
form.formAddRoster i can use $setPristine() in controller, but wwith
formAddRoster``''这两个名字。
所以我希望我能确切知道这个问题的根源...
这是我的代码:
* HTML:
<div ng-if="staff_array.length > 0">
<div ng-form="form.formAddRoster">
<input type="text" name="add_ros_1" class="form-control" ng-class="{invalid: formAddRoster.add_ros_1.$touched && formAddRoster.add_ros_1.$invalid}" ng-required="true"/>
<div ng-messages="formAddRoster.add_ros_1.$error" ng-if="formAddRoster.add_ros_1.$touched">
<div ng-message="required">Information could not leave blank</div>
</div>
</div>
</div>
* JS:
$scope.form = {};
$scope.addRoster = function(){
$scope.formAddRoster.$setPristine(); //This will not work with error cannot set with undefined
$scope.form.formAddRoster.$setPristine();//This work fine
};