0

I've a mistake with input type url in 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

    <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

1 Answer 1

1

The quickest way is adding required to input please see demo below.

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.