[英]only show button if input fields are not empty and one has valid URL
I have the following code and it works fine to check if the input field "inputURL" has a valid URL, if it has the anchor link a.btn
shows fine but I also only want it to show if both have been filled in. How can I add to the ng-show
in the anchor to do this? 我有以下代码,并且可以很好地检查输入字段“ inputURL”是否具有有效的URL,如果它具有锚链接a.btn
显示,但是我也只希望它们都被填充。我可以在锚中添加ng-show
来做到这一点吗?
<form name="myForm" class="row inputs">
<div class="col-xs-5">
<label for="exampleInputPassword1">Enter a Title/Description</label>
<input type="text" id="urlName" class="form-control" placeholder="" ng-model="mvName" >
</div>
<div class="col-xs-5">
<label for="exampleInputPassword1">Enter a URL</label>
<input type="url" name="inputURL" id="urlLink" class="form-control" placeholder="" ng-model="mvUrl" >
</div>
<div class="col-xs-2">
<a href="javascript:" ng-click="saveToList($event)" class="btn btn-block post" ng-show="myForm.inputURL.$valid ">Post</a>
</div>
</form>
You can check if both have been filled with the model like this: 您可以检查两个模型是否都已填充,如下所示:
ng-show="myForm.inputURL.$valid && mvName && nvUrl"
or with the view value of the input 或输入的视图值
ng-show="myForm.inputURL.$valid && myForm.inputURL.$viewValue && myForm.urlName.$viewValue"
You can use the required HTML5 special attribute to check if the input is not empy. 您可以使用必需的HTML5特殊属性来检查输入是否不是empy。
<form name="myForm" class="row inputs">
<div class="col-xs-5">
<label for="exampleInputPassword1">Enter a Title/Description</label>
<input type="text" name="mvName id="urlName" class="form-control" placeholder="" ng-model="mvName" required>
</div>
<div class="col-xs-5">
<label for="exampleInputPassword1">Enter a URL</label>
<input type="url" name="inputURL" id="urlLink" class="form-control" placeholder="" ng-model="mvUrl" required>
</div>
<div class="col-xs-2">
<a href="javascript:" ng-click="saveToList($event)" class="btn btn-block post" ng-show="myForm.inputURL.$valid && !myForm.inputURL.$error.required && !myForm.mvName.$error.required">Post</a>
</div>
</form>
However, to clean the view a little bit, I would suggest using a controller. 但是,为了稍微清理一下视图,我建议使用控制器。 Expose a function which returns true or false if the form is valid. 公开一个函数,如果该表单有效,则该函数返回true或false。 And simply use ng-show="isValid() ". 只需使用ng-show =“ isValid() ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.