【发布时间】:2020-01-04 14:45:43
【问题描述】:
我正在尝试从后端以动态方式为 div 设置 background-image。
我使用django (djangorestframework) 作为后端。
我正在发送一个 http 请求,这是我收到的数据:
{
"user": "1ad54d3c-a012-431a-9e7a-8630fd9fb566",
"image": "api/media/users/1ad54d3c-a012-431a-9e7a-8630fd9fb566/tutorials/yyyy/_image/tutorialImage.png",
"title": "yyyy",
"description": "hgdhgdh",
"level": "professional",
"parts": 0,
"offical": false
}
这是html:
<div class="tutorial-card">
<div class='tutorial-img' [style.background-image]="getTutorialImgURL(tutorial.image)">
</div>
getTutorialImageURL 函数:
public getTutorialImgURL(path: string){
return `url("http://${config.backendDomain}/${path}")`;
} // config.backendDomain is "localhost:8000"
好像没有向后端发送get请求(url路径是对的)
我尝试过的事情:
-
将前缀从
http更改为“https”- 它可以工作,但后端还不支持
https,所以它返回code 400
- 它可以工作,但后端还不支持
-
使用
[ngStyle]="{'background-image': getTutorialImgURL(tutorial.image)}"- 路径仍然正确,但不发送请求
- 使
getTutorialImgURL返回完整样式(returnimage-backgr: url("http://${config.backendDomain}/${path}")`)- 与所有不发送 http 请求的正确路径相同
- 使用
DomSanitizer(return this.sanitizer.bypassSecurityTrustStyle(url("http://${config.backendDomain}/${path}"));)- 还是和以前一样:正确的路径没有向后端发送 http 请求
(我不想使用<img [src]='...'>)
【问题讨论】:
标签: css django angular frontend backend