简体   繁体   English

在 Angular 中动态显示图像的问题

[英]Problem with Showing dynamically an image in Angular

So I have a list of doctors and I want to display for each doctor in the list the corresponding image.所以我有一个医生列表,我想为列表中的每个医生显示相应的图像。 The doctors have an ID so I tried to make the path for the image on ng-init but it still does not work.医生有一个 ID,所以我尝试在 ng-init 上为图像创建路径,但它仍然不起作用。 Can someone help me out?有人可以帮我吗? Here's the code.My images are stored in the assets/img folder and the corresponding image for every doctor matches the id of the doctor (Example: Doctor 1 with the id 1 has the image "1.jpg" etc. Also, is there any easier way to do this? Thank you!这是代码。我的图像存储在 assets/img 文件夹中,每个医生的相应图像与医生的 id 匹配(例如:id 为 1 的医生 1 具有图像“1.jpg”等。还有有更简单的方法吗?谢谢!

<li *ngFor="let doctor of doctors">
      <div class="row">
        <div class="col-4">
          <div ng-init="ImgPath ='assets/img/' + doctor.doctorId +'.jpg"></div>
          <img ng-src="{{ImgPath}" alt="" />
        </div>

(...) (...)

Supereasy: You can use only this: Supereasy:你只能使用这个:

<img src="assets/img/{{doctor.doctorId}}.jpg"/>

or you can bind it on.ts file或者你可以将它绑定到.ts 文件

html: html:

<img [src]="urlImage(doctor)"/>

ts: ts:

urlImage(doctor) {
  return `assets/img/${doctor.doctorId}.jpg`;
}

ng-init and ng-src are directives of AngularJS! ng-initng-src是 AngularJS 的指令! Don't use it in Angular >2请勿在 Angular >2 中使用

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

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