我开始使用Angular JS并尝试验证表单。但是,如果选中复选框,我就不知道如何验证某些输入。
<body>
<div ng-app="">
<form name="myForm">
<input name="myName" ng-model="myName" required>
<span ng-show="myForm.myName.$touched && myForm.myName.$invalid">The name is required.</span><br>
<input name="myphone" ng-model="myphone"><!--NO REQUIRED--><br>
Check to Validate:
<input type="checkbox" ng-model="myVar"><br>
<input type="text"><!--VALIDATE IF checkbox is checked--><br>
<input type="text"><!--VALIDATE IF checkbox is checked--><br>
</form>
<input type="submit">
</div>
如果出现错误,如何禁用输入?
答案 0 :(得分:1)
您可以使用ng-required
指令。
<div ng-app="">
<form name="myForm">
<input name="myName" ng-model="myName" required>
<span ng-show="myForm.myName.$touched && myForm.myName.$invalid">The name is
required.</span><br>
<input name="myphone" ng-model="myphone"><!--NO REQUIRED--><br>
Check to Validate:
<input type="checkbox" ng-model="myVar"><br>
<input type="text" ng-if="myVar"> <!-- this show ups -->
<input type="text" ng-required="myVar">
<span ng-show="myForm.$invalid">
<input type="text" ng-required="myVar">
<span ng-show="myForm.$invalid">
</form>
<input type="submit">
</div>
<强> CONTROLLER 强>
在控制器中,您可以根据模型签名
进行检查$scope.submitFunction = function(){
if($scope.myVar){
//do stuff
}
}