Html 动态添加图像
我正在构建一个有角度的应用程序。我需要为每个按钮添加不同的图像 HTML:Html 动态添加图像,html,angular,typescript,Html,Angular,Typescript,我正在构建一个有角度的应用程序。我需要为每个按钮添加不同的图像 HTML: 根据上面的代码,我创建的按钮取决于响应(例如:四个按钮),并且所有按钮都包含相同的图像。如何将不同的图像添加到每个ngFor按钮?您可以使用输入绑定[src]来指定源属性。假设Items具有imgSrc属性: <div *ngFor="let item of myItems"> <button class="close-image"><img [src]="item.imgSrc"&
根据上面的代码,我创建的按钮取决于响应(例如:四个按钮),并且所有按钮都包含相同的图像。如何将不同的图像添加到每个
ngFor
按钮?您可以使用输入绑定[src]
来指定源属性。假设Items
具有imgSrc
属性:
<div *ngFor="let item of myItems">
<button class="close-image"><img [src]="item.imgSrc">
<span>{{item.text}}</span>
</button>
</div>
这假设myItems
看起来像这样(无论是来自服务器还是硬编码):
您可以使用输入绑定
[src]
从属性指定源。假设Items
具有imgSrc
属性:
<div *ngFor="let item of myItems">
<button class="close-image"><img [src]="item.imgSrc">
<span>{{item.text}}</span>
</button>
</div>
这假设myItems
看起来像这样(无论是来自服务器还是硬编码):
html
html
在.ts文件中以数组形式保存图像路径。然后使用ngforhave为
myItems
创建模型,如果是这样的话。您可以将图像字段添加到该模式。获取myItems
值时,可以将图像值指定给该键。因此,在标记中,您可以获得图像
,与@Explosion Pills的回答相同。将图像路径保存在.ts文件的数组中。然后使用ngforhave为myItems
创建模型,如果是这样的话。您可以将图像字段添加到该模式。获取myItems
值时,可以将图像值指定给该键。因此,在标记中,您可以得到与@Explosion Pills'答案相同的图像
[src]="'../../assets/img/' + item.imgSrc"
myItems = [
{ imgSrc: 'flower.png', text: 'Flower' },
{ imgSrc: 'flower2.png', text: 'Flower2' },
];
<img *ngFor="let image of images" [src]="image">
images = [
'path/to/image/1',
'path/to/image/2',
'path/to/image/3',
];