在angular2中插入相对路径的图像

spl*_*unk 3 angular

我正在尝试在angular2中插入图像.我的图片名称保存在mypicture变量中.我试过这种方式,<img [src]="../../../public/img/{{mypicture}}" />但它没有用.

我在控制台中看到此错误:

Error: Uncaught (in promise): Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected
Run Code Online (Sandbox Code Playgroud)

Taw*_*wah 9

而不是在绑定之前联系完整路径

例如:

public fullPath:string;
public myPicture:string;

getMyPicture(){
   this.fullPath = "../../../public/img/"+ this.myPicture;
 }
Run Code Online (Sandbox Code Playgroud)

HTML

 <img [src]="fullPath" />
Run Code Online (Sandbox Code Playgroud)


小智 7

你只需要混合"和".

<img [src]="'../../../public/img/' + mypicture" />
Run Code Online (Sandbox Code Playgroud)

而已.快速简便的修复.