Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ng-repeat variable inside src attribute in angularjs?

I have the following:

div.container
    script(src='/js/bootstrap/holder.js')
    p.text-info(ng-pluralize,
                count="devices.length",
                when="{ '0': 'There are no fragments.', 'one': 'There is 1 fragment.', 'other': 'There are {} fragments.'}")

    ul.unstyled(ng-repeat='fragment in devices')
        ul.thumbnails
            li.span
                div.thumbnail
                    img(src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}")
                    div.caption
                        h3 {{fragment.name}}
                        p {{fragment.dimension}}
                        ul.unstyled(ng-repeat='component in fragment.components')
                            li {{ component.name }}

The problem is in src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}", i.e. even if looking at the resulting html I see a correct src (src="holder.js/300x200") it does not show the image. My guess is this is not how one uses angular variables inside attributes..

How can I make it work?

EDIT: it seems it does not execute holder.js.. here is the source: in the first call I used angular {{hash}} in the second I manually put holder.js/300x200

<div class="thumbnail">
    <img src="holder.js/1678x638">
    <img src="data:image/png;base64,iVBORw0KG...ElFTkSuQmCC" data-src="holder.js/300x200" alt="300x200" style="width: 300px; height: 200px;">
</div>
like image 619
fusio Avatar asked Aug 01 '13 14:08

fusio


People also ask

What is ng-repeat directive in AngularJS?

Angular-JS ng-repeat Directive Last Updated : 09 Aug, 2019 Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects.

When to use ng-SRC instead of SRC in AngularJS?

The ng-src directive should be used instead of src if you have AngularJS code inside the src value. The ng-src directive makes sure the image is not displayed wrong before AngularJS has evaluated the code. Supported by the <img> element. A string value, or an expression resulting in a string.

What are the limitations of array iteration in AngularJS?

However, there are a few limitations compared to array iteration: The JavaScript specification does not define the order of keys returned for an object, so AngularJS relies on the order returned by the browser when running for key in myObj.

How do I repeat a specific element in ngrepeat?

Special repeat start and end points. To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending the range of the repeater by defining explicit start and end points by using ng-repeat-start and ng-repeat-end respectively.


1 Answers

The documentation explains that quite clearly:

Using Angular markup like {{hash}} in a src attribute doesn't work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrc directive solves this problem.

So you must use:

<img ng-src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}" />
like image 144
Blackhole Avatar answered Oct 19 '22 15:10

Blackhole