【发布时间】:2018-09-26 03:21:59
【问题描述】:
我正在使用 Angular 5,我正在尝试在加载 div 的背景图像时获取加载图标。
如果它是正常的img,我对此没有任何问题,但如果我尝试将它作为背景,它就不起作用。
这里是一些示例代码
app.component.html
<div class="test" [someDirective]="'some/otherImg.jpg'"></div>
<img class="test" [someDirective]="'some/OtherImg.jpg'">
app.component.css
.test{
width: 300px;
height: 200px;
}
some.directive.ts
import { Directive, ViewChild, Input, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[someDirective]'
})
export class SomeDirective {
@Input() someDirective:string;
constructor(private element:ElementRef){
element.nativeElement.src = 'some/imgUrl.gif';
element.nativeElement.style = "background-image: url('some/imgUrl.gif')";
}
@HostListener('load', ['$event'])
onLoad() {
this.element.nativeElement.src = "some/otherImg.jpg";
this.element.nativeElement.style = "background-image: url('some/otherImg.jpg')";
}
}
有没有办法让它工作,以便背景也以与普通图像相同的方式加载?
我应该提到,让加载 gif 充当背景图像没有问题,它工作正常但不会改变。
【问题讨论】:
-
尝试写一个绝对网址...我的意思是../some/img.jpg
-
我的错我应该更具体地解决这个问题。网址不是问题。它们只是示例而不是真正的网址。在制作示例时我应该多加注意......对不起
-
的加载图标和图像工作正常,它只是
,它的背景图像会导致问题,因为看起来 (load) 只检查内容是否已加载并且不是风格你没有关闭图片标签 / 丢失了你找到解决办法了吗?
标签: angular typescript