0

I have used ng-src on img tags many times within ng-repeats with success, but for some reason I can't get this one to work.

I have an API call that returns some data, and for each item in the return, I basically do this:

<div ng-repeat="item in APIreturn">
    <img ng-src="{{item.url}}" />
</div>

I have also tried src="{{item.url}}", ng-src="item.url" and so forth. I've verified that the url that is being used is valid, and works fine in src="{{item.url}}" outside of the ng-repeat.

Any ideas why it would be different in the ng-repeat?

Current HTML

<table ng-repeat="user in userInformation">
    <tr>
        <td>UserName:</td>
        <td>{{user.UserName}}</td>
    </tr>
    <tr>
        <td>Address:</td>
        <td>{{user.Address}}</td>
    </tr>
    <tr>
        <td>Gender:</td>
        <td>{{user.Gender}}</td>
    </tr>
    <tr>
        <td>Image:</td>
        <td><img ng-src"user.image" /></td>
    </tr>
</table>

These are both using the same element in this object...but for some reason the image render

6
  • The final HTML might be helpful. Commented Jun 9, 2016 at 20:00
  • Just posted it for you. Thanks for the help. Commented Jun 9, 2016 at 20:05
  • Do you see url if you use just <td>{{ user.image }}</td>? user.image or user.Image? Commented Jun 9, 2016 at 20:06
  • change to <img ng-src= "user.image" /> Commented Jun 9, 2016 at 20:08
  • Sorry, yes it is showing. But the same attribute doesn't show the image in the image tag. Commented Jun 9, 2016 at 20:08

4 Answers 4

2

You say you don't see the url if you put just <td>{{ user.image }}</td>. This indicate you access not existing property on yout object. Try json filter:

<td>{{ user | json }}</td>

to see more of your object. As I can see, you use capital letters, but user.image is lower cased.

EDIT: here is a working plunkr.

EDIT2: you missing assign sign.

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

3 Comments

I was mistaken, and tried to correct my comment. The image that I just posted above shows your suggestion of printing just the url. It is being printed, and the img src is using the exact same attribute that is being printed below it.
See my edit2. You wrote ng-src"{{ instead of ng-src="{{
Thanks Anton! That was it.
2

try change to this. i think you forgot put = after ng-src .

  <tr>
    <td>Image:</td>
    <td><img ng-src = "user.image" /></td>
  </tr>

1 Comment

Thank you for catching that. That was it.
0

See Strict Contextual Escaping service.

Angular unsafes img src attributes so you'll need to trust it in order to make your images available.

Another approach is to use css to render the images:

Comments

0

For table, in ng-repeat, you have to use {{}} this interpolation in order to access the src file.
Try this one:

<td><img ng-src = "{{user.image}}"/></td>

Let me know if any issues.

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.