क्या आपने इन आधिकारिक डॉक्स की जाँच की है?
HostListener - एक मेजबान श्रोता की घोषणा करता है। जब कोणीय तत्व निर्दिष्ट घटना का उत्सर्जन करता है, तो कोणीय सुशोभित विधि को लागू करेगा।
@HostListener- मेजबान तत्व द्वारा उत्सर्जित घटना को सुनेंगे, जिसके साथ घोषित किया गया है @HostListener।
HostBinding - एक मेजबान संपत्ति बाध्यकारी घोषित करता है। परिवर्तन का पता लगाने के दौरान कोणीय स्वचालित रूप से होस्ट प्रॉपर्टी बाइंडिंग की जाँच करता है। यदि कोई बाइंडिंग बदलता है, तो यह निर्देश के होस्ट तत्व को अद्यतन करेगा।
@HostBinding- मेजबान तत्व को संपत्ति बाँध देगा, यदि कोई बाध्यकारी परिवर्तन करता है, HostBindingतो मेजबान तत्व को अद्यतन करेगा।
नोट: हाल ही में दोनों लिंक हटा दिए गए हैं। स्टाइल गाइड के " HostBinding-HostListening " भाग लिंक लौटने तक एक उपयोगी विकल्प हो सकता है।
चित्र का अर्थ करने के लिए यहां एक सरल कोड उदाहरण दिया गया है:
डेमो: यहाँ डेमो लाइव प्लंकर में है - "@HostListener & @Hostinding के बारे में एक सरल उदाहरण"
- यह उदाहरण मेजबान के तत्व के
roleसाथ घोषित - एक संपत्ति को बांधता है@HostBinding
- याद रखें कि
roleएक विशेषता है, क्योंकि हम उपयोग कर रहे हैं attr.role।
<p myDir><p mydir="" role="admin">जब आप इसे डेवलपर टूल में देखते हैं तो यह बन जाता है ।
- यह तब घटक के होस्ट तत्व के साथ, प्रत्येक क्लिक के साथ बदलते हुए
onClickघोषित की गई घटना को सुनता है ।
@HostListenerrole
- जब
<p myDir>क्लिक किया जाता है तो परिवर्तन यह होता है कि इसके उद्घाटन टैग से और पीछे से परिवर्तन होता <p mydir="" role="admin">है <p mydir="" role="guest">।
directives.ts
import {Component,HostListener,Directive,HostBinding,Input} from '@angular/core';
@Directive({selector: '[myDir]'})
export class HostDirective {
@HostBinding('attr.role') role = 'admin';
@HostListener('click') onClick() {
this.role= this.role === 'admin' ? 'guest' : 'admin';
}
}
AppComponent.ts
import { Component,ElementRef,ViewChild } from '@angular/core';
import {HostDirective} from './directives';
@Component({
selector: 'my-app',
template:
`
<p myDir>Host Element
<br><br>
We have a (HostListener) listening to this host's <b>click event</b> declared with @HostListener
<br><br>
And we have a (HostBinding) binding <b>the role property</b> to host element declared with @HostBinding
and checking host's property binding updates.
If any property change is found I will update it.
</p>
<div>View this change in the DOM of the host element by opening developer tools,
clicking the host element in the UI.
The role attribute's changes will be visible in the DOM.</div>
`,
directives: [HostDirective]
})
export class AppComponent {}