Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Component host property

Tags:

angular

What does @Component.host property stand for?

According to Angular2 documentation it stands for:

host - map of class property to host element bindings for events, properties and attributes.

I don't quite figure out what's it for?

I'm posing this in order to understand a stuff code I've stuck last days.

The code is:

@Component({   selector: 'layout',   encapsulation: ViewEncapsulation.None,   templateUrl: './layout.template.html',   host: {     '[class.nav-static]' : 'config.state["nav-static"]',     '[class.chat-sidebar-opened]' : 'chatOpened',     '[class.app]' : 'true',     id: 'app'   } }) export class Layout { 
like image 359
Jordi Avatar asked Dec 15 '16 09:12

Jordi


People also ask

What is host in Angular component?

Every component is associated within an element that matches the component's selector. This element, into which the template is rendered, is called the host element. The :host pseudo-class selector may be used to create styles that target the host element itself, as opposed to targeting elements inside the host.

What is host property in Angular?

The host property is used to bind properties, attributes, and events to that particular class component, using a set of key-value pairs. host: { [key: string]: string; } Angular automatically checks host property bindings during change detection. If a binding changes, Angular updates the directive's host element.

What is the use of host ()?

The @Host property searches for the dependency inside the component's template only. It starts with the current Injector and continues to search in the Injector hierarchy until it reaches the host element of the current component. It does not search for the dependency in the Providers of the host element.

What is host in directive?

The host property, as mentioned in the documentation. Maps class properties to host element bindings for properties, attributes, and events, using a set of key-value pairs. where the host element is the element/component you have attached your directive to.


1 Answers

I have added class to host tag.

Like following:

  • Component

     @Component({   selector: 'mytag',   templateUrl: './layout.template.html',   host: {     'class' : 'myclass1 myclass2 myclass3'   } }) export class MyTagComponent { 
  • View code

    <mytag></mytag>

  • Result

    <mytag class="myclass1 myclass2 myclass3"></mytag>

like image 53
Saiki Koji Avatar answered Oct 18 '22 19:10

Saiki Koji