@HostBinding and @HostListener: what do they do and what are they for?

前端 未结 7 900
[愿得一人]
[愿得一人] 2020-11-30 16:57

In my meanderings around the world wide interweb, and now especially the angular.io style docs, I find many references to @HostBinding and @HostListener

相关标签:
7条回答
  • 2020-11-30 17:58

    One thing that adds confusion to this subject is the idea of decorators is not made very clear, and when we consider something like...

    @HostBinding('attr.something') 
    get something() { 
        return this.somethingElse; 
     }
    

    It works, because it is a get accessor. You couldn't use a function equivalent:

    @HostBinding('attr.something') 
    something() { 
        return this.somethingElse; 
     }
    

    Otherwise, the benefit of using @HostBinding is it assures change detection is run when the bound value changes.

    0 讨论(0)
提交回复
热议问题