5

So basically when I was using native html5 i was using five <img src=""> tags to load five images. When i wanted to use AngularJS i thought of iterating a div five times through ng-repeat and using ng-src="" get the image whose source path is stored in an array in the controller. But some how im always getting this error GET file:///home/sanad/Desktop/drag/x.source net::ERR_FILE_NOT_FOUND

This is my html:

<div ng-repeat="x in img">{{x.source}}
    <img ng-id="x.id" ng-src="x.source" >
</div>

This is my js:

var app=angular.module('d2d',[]);
app.controller('imageCtrl',function($scope){
    $scope.img=[
        {id:'table',source:'images/table.jpg'},
        {id:'paper',source:'images/paper.jpg'},
        {id:'computer',source:'images/computer.jpg'},
        {id:'ac',source:'images/ac.jpg'},
        {id:'sofa',source:'images/sofa.jpg'}
    ];
});
2
  • 2
    Try use <img ng-src="{{x.source}}"> Commented Dec 29, 2014 at 10:23
  • you might want to set the image with css instead using an image. Angularjs have security policies that might disable ng-src on images inside ng-repeat: <div data-ng-repeat="i in images"><div style="background:url(imgs/{{i.srcimg}})"></div></div> Commented Jun 26, 2015 at 2:35

3 Answers 3

9
 <div  ng-repeat="x in img">
     <img ng-src="{{x.source}}" >
 </div>`

I'm not sure about what ng-src does so I did not suggested it in first place.

I also have the feeling that using a directive for doing something that html does well is not a good approach. In fact, it instantiates new scopes, new watches,...

Edit:

Using src="{{param}}" instead of ng-src="{{param}}" could lead the browser to make a call to the url http:\\yourServer\{{param}}, which would be invalid. ng-src has been created to solve that problem.

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

2 Comments

According to the AngularJS API, this is a "buggy way to do it". The correct is to use mixing your answer with the question. <img ng-src="{{x.source}}"/>. Ref: Angular API
FYI ng-src does not create a new scope or a new watch. A watch is added by virtue of the interpolation ({{}}) in both cases. There is no extra watch from one solution to the other. Also, both solutions do not create additional scope.
3

You can do this too

<img ng-repeat="x in img" src="{{x}}">

it works properly

Comments

0
<img ng-repeat="x in img" ng-src="{{x.source}}">

If you have just an array of images

<img ng-repeat="img in images track by $index" ng-src="{{img}}">

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.