0

I need one help.I am getting the following error while assigning value to model using angular.js.

Error:

angularjs.js:107 TypeError: Cannot set property '0' of undefined

I am explaining my code below.

 <div class="input-group bmargindiv1 col-md-12" ng-repeat="mul in mulImage">
 <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Image{{$index+1}}:</span>
 <div>
 <div ng-class="{'myError': billdata.upload_{{$index}}.$touched && billdata.upload_{{$index}}.$invalid }">
 <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-min-height="400" ngf-resize="{width: 400, height:400}"  ngf-select="onFileSelect1('upload_{{$index}}',mul.image);">
 <div style="clear:both;"></div>
 <input type="text" id="hidn_{{$index}}" ng-model="attach[$index]" style="display:none" />
</div>
 </div>
<span class="input-group-btn" ng-show="mulImage.length>0">
 <img ngf-thumbnail="mul.image" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;" ng-if="mul.image !=null"><img ng-src="dis_{{$index}}" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;" ng-if="attach[$index]!=''">
<input type="button" class="btn btn-success" name="plus" id="plus" value="+" ng-click="addNewImageRow(mulImage);" ng-show="$last"> <input type="button" class="btn btn-danger" name="minus" id="minus" value="-"  ng-show="mulImage.length>1" ng-click="deleteNewImageRow(mulImage,$index);">

 </span>
 </div>

Here i have multiple image option.I have some images i need to display those into image tag but here i have two condition if user is selecting image from local disc then first image tag will active and when there is existing image other image tag will active.i am explaining my controller code below.

var multiImage=response.data[0].multiple_image;
            var array=multiImage.split(",");
            for(var i=0;i<array.length;i++){
                 $scope.mulImage.push({'image':null});
                 $scope.attach[i]=array[i];
                 $scope.dis_i="upload/"+array[i];
}        

multiImage variable giving the output like this ['abc.jpg','def.jpg'].here i need to set image name to this input <input type="text" id="hidn_{{$index}}" ng-model="attach[$index]" style="display:none" /> field but getting error at this $scope.attach[i]=array[i]; line.Please help me to resolve this error so that code should work as per my requirement.

10
  • Did you declare $scope.attach? You can declare it using $scope.attach = []; Commented Apr 15, 2016 at 5:41
  • No,i have not declared.let me to do that. Commented Apr 15, 2016 at 5:43
  • That should fix your issue.. Commented Apr 15, 2016 at 5:45
  • yes,error gone.but images are not displaying. Commented Apr 15, 2016 at 5:47
  • Are you seeing any other errors in console? Commented Apr 15, 2016 at 5:49

1 Answer 1

1

In case you don't need the attach array in another place, you can simplify your code and make it work with these changes:

First, remove the attach array and the dis_i variable (which, by the way, cannot work because it would contain the url of the last image, as at each step of the for loop you set its value to array[i] and not each image in turn as you expect) and simply set the filename into the mulImage array's items:

    var multiImage=response.data[0].multiple_image;
    var array=multiImage.split(",");
    $scope.mulImage = [];
    for(var i=0;i<array.length;i++){
      $scope.mulImage.push({'image':null, 'filename':array[i]});
    }

Then edit your html to use the filename property of the mul object (coming from the array mulImage you are looping through):

<div class="input-group bmargindiv1 col-md-12" ng-repeat="mul in mulImage">
  <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Image{{$index+1}}:</span>
  <div>
    <div ng-class="{'myError': billdata.upload_{{$index}}.$touched && billdata.upload_{{$index}}.$invalid }">
      <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-min-height="400" ngf-resize="{width: 400, height:400}"  ngf-select="onFileSelect1('upload_{{$index}}',mul.image);">       
      <div style="clear:both;"></div>
      <input type="text" id="hidn_{{$index}}" ng-model="mul.filename" style="display:none" />
    </div>
  </div>
  <span class="input-group-btn" ng-show="mulImage.length>0">
    <img ngf-thumbnail="mul.image" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;" ng-if="mul.image !=null"><img ng-src="upload/{{mul.filename}}" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;" ng-if="mul.filename!=''">
    <input type="button" class="btn btn-success" name="plus" id="plus" value="+" ng-click="addNewImageRow(mulImage);" ng-show="$last"> <input type="button" class="btn btn-danger" name="minus" id="minus" value="-"  ng-show="mulImage.length>1" ng-click="deleteNewImageRow(mulImage,$index);">
  </span>
</div> 

This should solve your issue.

PS: I didn't remove the ng-show="mulImage.length>0" in your last span, but it's unnecessary as you'd never reach that code if mulImage doesn't contain anything because this span is inside the ng-repeat block.

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

9 Comments

ok,i will try this but another use-case is if user is selecting image from local drive the first image tag should active.How that will execute.
What do you mean by the first image tag should be active? Is it the thumbnail one? Are you trying to show a thumbnail of the image selected by the user on his local disk before uploading it to the server?
yes,you way is correct but let me to test another use cases.
this ` <img ngf-thumbnail="mul.image" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;" ng-if="mul.image !=null">` will active and other image tag will disable when user is selecting image from local drive.
I don't really understand your point, but if you're talking about showing a preview of the image the user selects before he uploads it to your server, then this question might help link. It not angular code, but you could easily encapsulate it in a directive.
|

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.