问题描述
如何检查给定的输入控件是否为空?我知道该字段上有 $pristine
属性,该属性表明给定字段最初是否为空,但是如果有人填写该字段并再次拉取整个内容怎么办?
How can I check if a given input control is empty? I know there is $pristine
property on the field which tells that if a given field is empty initially but what if when someone fill the field and yanks the whole content again?
我认为上述功能是必要的,因为它告诉用户该字段是必需的.
I think above feature is necessary as its important for telling the user that field is required.
任何想法将不胜感激!
推荐答案
很简单:
<input ng-model="somefield">
<span ng-show="!somefield.length">Please enter something!</span>
<span ng-show="somefield.length">Good boy!</span>
您也可以使用 ng-hide="somefield.length"
而不是 ng-show="!somefield.length"
如果这对您来说更自然.
You could also use ng-hide="somefield.length"
instead of ng-show="!somefield.length"
if that reads more naturally for you.
更好的选择可能是真正利用 Angular 的表单功能:
A better alternative might be to really take advantage of the form abilities of Angular:
<form name="myform">
<input name="myfield" ng-model="somefield" ng-minlength="5" required>
<span ng-show="myform.myfield.$error.required">Please enter something!</span>
<span ng-show="!myform.myfield.$error.required">Good boy!</span>
</form>
这篇关于检查输入框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!