简体   繁体   English

指令hoslistener到带有加载事件的图像标签-angular4

[英]directive hoslistener to image tag with load event - angular4

I'm trying to get a directive using hostlistener to detect when images are completed loaded with angular4 我正在尝试使用hostlistener获取指令,以检测何时完成用angular4加载的图像

    <img imgDirective src="124.jpg">

    @HostListener('load', ['$event.target']) 
    public onLoad(event) {
        this.img = false;
    }

Then if the load is complete I output the value using an EventEmitter, however, the hostlistener event seems not working at all. 然后,如果加载完成,我将使用EventEmitter输出值,但是,hostlistener事件似乎根本不起作用。 Does any one knows why?if I a change the event on the hostlistener as below? 有谁知道为什么吗?如果我按以下方式更改主持人监听器上的事件? the event works, but not for image tag. 该事件有效,但不适用于图像标签。 Do you know if this is possible? 你知道这有可能吗?

    @HostListener('window:load', ['$event.target']) 
    public onLoad(event) {
        this.img = false;
    } 

I got the event form https://developer.mozilla.org/en-US/docs/Web/Events 我收到了活动表格https://developer.mozilla.org/en-US/docs/Web/Events

What events can we use in angular4? 我们可以在angular4中使用哪些事件? is a limited list? 是一个有限的清单?

I would do it this way: 我会这样:

@Directive({
  selector: '[imgDirective]',
})
class ImgDirective {
  @Input() imgDirective:string;

  constructor(private element:ElementRef){
    element.nativeElement.src = 'resources/spinner.png';
  } 

  @HostListener('load', ['$event'])
  onLoad() {
    this.element.nativeElement.src = this.imgDirective;
  }
}

and use it like 并像这样使用

<img [imgDirective]="124.jpg">

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

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