[英]AngularJS input url invalid/valid/error
I've a mistake with input type url in angularjs. 我在angularjs中输入类型的网址有误。 I would disable the button if input is empty or invalid and I wrote the code below. 如果输入为空或无效,我将禁用该按钮,并在下面编写了代码。 Well, it's fine on the first time o if I write an incorrect url but if it's empty propForm.imgUrl.$invalid and propForm.imgUrl.$error.url it returns me false and this enable click of button 好吧,如果我输入的网址不正确,那么第一次还是可以的,但是如果它是空的propForm.imgUrl。$ invalid和propForm.imgUrl。$ error.url,它会返回false并启用按钮单击
<div class="col-sm-10">
<input type="url" name="imgUrl" ng-model="$parent.urlImage" class="form-control">
</div>
<div class="pull-right col-sm-2">
<a href="" class="btn btn-default btn-primary" ng-disabled="propForm.imgUrl.$invalid || propForm.imgUrl.$pristine || propForm.imgUrl.$error.url" ng-click="addImage()">Add</a>
</div>
Where is the trick?a 诀窍在哪里?
The quickest way is adding required
to input
please see demo below. 最快的方法是添加required
的input
请参阅下面的演示。
var app = angular.module('app', []); app.controller('homeCtrl', function($scope) { });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <div ng-app="app"> <div ng-controller="homeCtrl"> <form name="propForm"> <div class="col-sm-10"> <input type="url" name="imgUrl" ng-model="$parent.urlImage" class="form-control" required> </div> <div class="pull-right col-sm-2"> <a href="" class="btn btn-default btn-primary" ng-disabled="propForm.imgUrl.$invalid || propForm.imgUrl.$pristine || propForm.imgUrl.$error.url" ng-click="addImage()">Add</a> </div> </form> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.