If I have the Component class
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
}
and index.html is
<div>
<my-app></my-app>
</div>
What would be the host element of the component? Would it be <div>
or <my-app>
? If it is <my-app>
, is there a way to get access to the <div>
(parent of AppComponent
) from AppComponent
?
:host is used to address the hosting element - that is the one that you use to add the component somewhere (e.g. <app-component> ). Use the :host pseudo-class selector to target styles in the element that hosts the component (as opposed to targeting elements inside the component's template).
:host() The :host() CSS pseudo-class function selects the shadow host of the shadow DOM containing the CSS it is used inside (so you can select a custom element from inside its shadow DOM) — but only if the selector given as the function's parameter matches the shadow host.
HostListener - Declares a host listener. Angular will invoke the decorated method when the host element emits the specified event. @HostListener - will listen to the event emitted by the host element that's declared with @HostListener . HostBinding - Declares a host property binding.
A component is a directive with a template that allows building blocks of a UI in an Angular application. Components will always have a template, a selector, and may or may not have a separate style.
For Directive, it is the element the directive is attached to. Eg h1
is the host element in below example
<h1 my-directive>
</my-comp>
For component, it is the selector of the component. Eg
selector: 'my-comp'
template: '
<h1> some heading </h1>
.....
<my-comp></my-comp> <!-- my-comp is the host element of h1 -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With