javascript 如何检查angularjs中的所有字段都是空的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29714416/
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
how to check all fields are empty in angularjs
提问by davidlee
How to check all fields in the submit form ng-submit="submit(field)"
is empty in controller.js? They consists of few textboxes and one select field.
如何ng-submit="submit(field)"
在controller.js 中检查提交表单中的所有字段是否为空?它们由几个文本框和一个选择字段组成。
I would want an alert box to occur if all fields are empty and select option is not selected, instead of individual validation.
如果所有字段都为空并且未选择选择选项,而不是单独验证,我希望出现一个警报框。
Thanks
谢谢
采纳答案by Dylan Watt
While it's not exactly what you're asking about, may be $pristine
is what you want? It's a flag on the form indicating whether the form has ever been edited. If someone typed and then cleared out a field, $pristine
would still be false, however.
虽然这不完全是您要问的,但可能$pristine
是您想要的?它是表单上的一个标志,指示该表单是否曾经被编辑过。但是,如果有人输入然后清除了一个字段,$pristine
仍然是错误的。
<form name="myform" ng-submit="doSubmit()" ng-controller="FormController">
<input ng-model="firstName" name="firstName" />
</form>
Then in your controller
然后在你的控制器中
.controller('FormController', function($scope){
$scope.doSubmit = function(){
if($scope.myform.$pristine){}
}
})
Alternatively, you can set all your fields to required="true"
and use the $valid
flag on the form in the same way as described above.
或者,您可以将所有字段设置为required="true"
并$valid
以与上述相同的方式使用表单上的标志。
回答by Scymex
The following will show an alert if all fields are empty
如果所有字段都为空,以下将显示警报
angular.module('app', [])
.controller('FormController', function($scope) {
$scope.doSubmit = function(){
if (formIsEmpty($scope.myform)){
alert('You forgot to enter something before submitting');
}
};
function formIsEmpty(form) {
for (var prop in form) {
if (!form.hasOwnProperty(prop)) { continue; }
if (prop[0] === '$') { continue; }
if ($scope[prop]) { return false; }
}
return true;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<form name="myform" ng-submit="doSubmit()" ng-controller="FormController">
<input ng-model="firstName" name="firstName" />
<input ng-model="lastName" name="lastName" />
<textarea ng-model="description" name="description"></textarea>
<button type="submit">Submit</button>
</form>
</div>
回答by Rahul Tripathi
You can use this:
你可以使用这个:
ng-show="!yourfield.length"
Something like this:
像这样的东西:
<form name="yourform">
<input name="yourfield" ng-model="somefield" ng-minlength="10" required>
<span ng-show="!yourfield.myfield.$error.required">Something</span>
</form>
回答by Ebad Masood
You should look for Form Validation. Take a look at this tutorial.
您应该寻找Form Validation。看看这个教程。
<input name="name" class="form-control"
ng-model="user.name" placeholder="Name" required>
<div class="alert alert-danger"
ng-show="userForm.name.$error.required">
Please enter the name.
</div>