Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a Host element in Angular for a Component

Tags:

angular6

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?

like image 773
Manu Chadha Avatar asked Aug 06 '18 17:08

Manu Chadha


People also ask

What does host mean in Angular?

: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).

What is the use of host ()?

: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.

What are host listeners in Angular?

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.

What should be a component in Angular?

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.


1 Answers

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 -->
like image 200
Manu Chadha Avatar answered Sep 28 '22 14:09

Manu Chadha