Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular loading img src in repeater without items

I have an angular repeater (table) which includes a src element. However, my URL gets called the first time before the items are loaded, causing one 404 load like this:

http://localhost:9000/%7B%7Bitem.obj.thumbnailurl%7D%7D 404 (Not Found)

The table looks like this (simplified):

<table>
    <thead>
        <tr>
            <th>Item title</th>
            <th>Link</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in items">
            <td>{{item.obj.itemtitle}}</td>
            <td>
                <a href="{{item.obj.landingpageurl}}">
                    <img src="{{item.obj.thumbnailurl}}"/>
                </a>
            </td>
        </tr>
    </thead>
</table>

The items array is loaded later, as a result of a search.

What happens is each time I open this view (even without page reloads, just opening other views) I get that 404 error.

Why is that happening? How can I avoid it?

like image 816
Zlatko Avatar asked Dec 25 '22 18:12

Zlatko


1 Answers

As CodeHater suggests, using ng-src will solve this issue. The problem is that the browser interprets {{item.obj.thumbnailurl}} as a real src attribute before angular has compiled the ng-repeat and its contents. This is also the reason why you get exactly one such error even though the img tag is inside a ng-repeat. When using ng-src, angular will insert the correct src attribute in your DOM the moment it compiles the ng-repeat and its contents.

like image 51
lex82 Avatar answered Jan 14 '23 13:01

lex82