Angularjs验证形式的ng-messages问题

时间:2019-07-04 01:44:35

标签: angularjs ng-messages angularjs-validation angularjs-ng-form

我正在使用Angularjs的ngMessages验证我的表单,当ng-form的名称是诸如ng-form="form.formAddRoster"之类的对象类型时,就会出现问题,这会导致ng-messages无法正常工作。我还尝试了form.formAddRosterformAddRoster``` 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
};

0 个答案:

没有答案