简体   繁体   English

Angular2-HTML中的动态变量

[英]Angular2 - dynamic variable in html

I have a list which looks like this: 我有一个看起来像这样的清单:

<ul *ngFor="#item of items">
<li><img src="http://something.com/[this needs to be item.id]/picture"></li>
</ul>

I need to the image url to be that of the id of the item. 我需要图片网址是商品ID的网址。 I looked at the src documentation for angular2 ( https://angular.io/docs/ts/latest/guide/template-syntax.html ) and tried the following: 我查看了angular2的src文档( https://angular.io/docs/ts/latest/guide/template-syntax.html ),并尝试了以下操作:

<img [src]="http://something.com/{{item.id}}/picture">

but it did not work. 但它没有用。 How can I have the image show up based on the item's id which needs to be in the url. 如何根据需要在网址中包含的商品ID显示图片。

Use property binding with string literals: 对字符串文字使用属性绑定:

<img [src]="'http://something.com/' + item.id + '/picture'">

or string interpolation property binding with {{}} s: 或与{{}}绑定的字符串插值属性:

<img src="http://something.com/{{item.id}}/picture">

Note that [src]="http://something.com/{{item.id}}/picture"> doesn't work because property binding with [] uses the syntax [property]="template_expression" and the template_expression can not contain {{}} s. 请注意, [src]="http://something.com/{{item.id}}/picture">无效,因为与[]属性绑定使用语法[property]="template_expression" ,而template_expression可以不包含{{}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM