Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 动态添加图像_Html_Angular_Typescript - Fatal编程技术网

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"&

我正在构建一个有角度的应用程序。我需要为每个按钮添加不同的图像

HTML:



根据上面的代码,我创建的按钮取决于响应(例如:四个按钮),并且所有按钮都包含相同的图像。如何将不同的图像添加到每个
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',
];