简体   繁体   中英

Can not validate the input field taking name dynamically using Angular.js

I need one help. Actually i am getting the following error while trying to validate the input field by taking the field name dynamically using Angular.js.

Error:

TypeError: Cannot read property '$invalid' of undefined

I am explaining my code below.

<form name="billdata" id="billdata"  enctype="multipart/form-data" novalidate>
   <div ng-repeat="mul in mulImage">
      <input type="file" class="filestyle form-control" data-size="lg" name="upload_{{$index}}" id="bannerimage_{{$index}}"  ng-model="mul.image" ngf-pattern="'image/*'" accept="image/*" ngf-max-size="2MB" ngf-select="onFileSelect1($index);">
   </div>
<input type="button" class="btn btn-success" ng-click="saveResturantDetails(billdata);"  id="saveData" value="Save"   style="margin-right:20px;"/>   </div>
</form>

Here i am uploading multiple images and when user will click on save button the it should first validate. I am explaining my controller side code below.

$scope.saveResturantDetails=function(billdata){
    if(billdata.$valid){
     }else{
      if(angular.isDefined($scope.mulImage)){
                for(var i=0;i<$scope.mulImage.length;i++){
                        var name='upload_'+i;
              if(billdata.name.$invalid){
                   alert('Please add the valid image(i.e-.png or .jpeg)  of size upto 2 mb max in image field'+(i+1));
                            return false;
                        }
                }
            }
     }
}

Actually i am getting the error in this if(billdata.name.$invalid) actually it can not take the proper name.If i am writing like this 'billdata.upload_0.$invalid' its working fine. But here i need to attach the name dynamically. Please help me.

这项工作会:

billdata[name].$invalid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM