variables - 在 angularjs 的 src 属性中使用 ng-repeat 变量?

标签 variables angularjs attributes angularjs-ng-repeat

我有以下几点:

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 }}

问题出在src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}" ,即即使查看生成的 html 我看到正确的 src ( src="holder.js/300x200" ) 它也不会显示图像。我的猜测是这不是在属性中使用 Angular 变量的方式..

我怎样才能让它工作?

编辑:
好像没有执行holder.js ..
这是来源:在第一个调用中我使用了 angular {{hash}} 在第二个中我手动放置了 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>

最佳答案

documentation非常清楚地解释了这一点:

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.



所以你必须使用:

<img ng-src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}" />

关于variables - 在 angularjs 的 src 属性中使用 ng-repeat 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996515/

相关文章:

javascript - AngularJS 函数

python - 如何使用实例属性来定义类属性?

object - 如何在 Raku 中动态访问对象中的属性

jquery - 如何根据 src 属性使用 Jquery 检查 DOM 中是否存在图像元素?

java - 如何将变量从 AsyncTask 返回到 Activity 中的 OnCreate()

python - 2-dim 字典,键为字符串

javascript - javascript中变量作用域的问题

python - 如何将列表的元素作为变量返回?

javascript - 过滤对象数组以返回具有最新日期属性的对象

angularjs - zsh : command not found: nx