javascript Angularjs 输入字段为空或未验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29100658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs input fields empty or not validation
提问by Dean Jason
<ul ng-repeat="fields in data">
<li>
<input type="text" ng-model="fields.field2.i">
</li>
<li>
<input type="text" ng-model="fields.field2.ii">
</li>
<li>
<input type="text" ng-model="fields.field2.iii">
</li>
</ul>
How to check all the fields is filled or not in angularjs? Check the ng-model or the json's object?
如何检查angularjs中的所有字段是否已填写?检查 ng-model 或 json 的对象?
回答by Norbor Illig
Check this plunker
检查这个plunker
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<form name="form">
<ul ng-repeat="fields in data">
<li>
<input type="text" ng-model="fields.field2.i" required>
</li>
<li>
<input type="text" ng-model="fields.field2.ii" required>
</li>
<li>
<input type="text" ng-model="fields.field2.iii" required>
</li>
</ul>
<button type="button" ng-disabled="form.$invalid" ng-click="save()">Save</button>
</form>
</body>
</html>
Basically, you should use 'required' on all the required fields and only allow the click on the save button if all fields are fulfilled. This is the very basic form of validation, for more information see this link
基本上,您应该在所有必填字段上使用“必需”,并且只有在所有字段都已填写时才允许单击保存按钮。这是非常基本的验证形式,有关更多信息,请参阅此链接