okla okla - 2 years ago 105
AngularJS Question

Angular: Why does field $invalid is true in other view?

I have added some condition to show my revision field. Basically it will accept a number and a required field , so i added some validation check. I have excluded this field from 'edit' view. and for that i specified condition as well.

ng-show="editMode=='addNew' || editMode=='addDate'"

even though in edit view my field is not showing up.. but somewhere $invalid is becoming true for all view

<div class="form-group" ng-show="editMode=='addNew' || editMode=='addDate'">
<label for="editmyRevision" ng-class="{'col-sm-3':editMode=='addNew'}" class="col-md-2 control-label">Revision</label>
<div ng-class="{'col-sm-9':editMode=='addNew'}" class="col-md-10">
<input class="form-control" ng-model="editmyRevision"name="revision" ng-required="editMode=='addNew' || editMode =='addDate'" ng-maxlength="5" ng-Pattern="/^[0-9]*$/"></input>
<span class="has-error help-block" data-ng-show="prodEdit.revision.$invalid">Acceptable formats: 100~99999</span>


What am i doing wrong here?

Answer Source

ng-show will not remove the input from the form. It will just set display:none style to it, so it will still make the form invalid.

Try using ng-if instead.

<div class="form-group" ng-if="editMode=='addNew' || editMode=='addDate'">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download