HostBinding – Stepan Suvorov Blog https://stepansuvorov.com/blog Release 2.0 Sat, 14 Jan 2017 21:16:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.3.1 Angular2: Possible parameters for @HostBinding and @HostListener decorators https://stepansuvorov.com/blog/2017/01/angular2-possible-parameters-for-hostbinding-and-hostlistener-decorators/ https://stepansuvorov.com/blog/2017/01/angular2-possible-parameters-for-hostbinding-and-hostlistener-decorators/#respond Sat, 14 Jan 2017 21:16:53 +0000 http://stepansuvorov.com/blog/?p=3214 Continue reading ]]> @HostBinding(?)
  • propertyName: references a property of the host with the propertyName name.
  • attr.attributeName: references an attribute of the host with the attributeName name. The initial value is set to the associated directive property. Setting a value in the property updates the attribute on the corresponding HTML element. Using the null value at this level removes the attribute on the HTML element.
  • style.styleName: links a directive property to a style of the HTML element.
  • class.className: links a directive property to a class name of the HTML element. If the value is true, the class is added otherwise removed.

@HostListener(?)

  • eventName: the name of the event to register a method callback on.

Example for all the cases:

[javascript]
@Directive({
selector: ‘mydir’
})
export class SomeDirective {
@HostBinding(‘value’) value:string;
@HostBinding(‘attr.role’) role:string;
@HostBinding(‘style.width.px’) width:number;
@HostBinding(‘class.someClass’) condition:boolean;
@HostListener(‘input’) onInput() {
console.log(‘on input in directive’);
}
}
[/javascript]

 

]]>
https://stepansuvorov.com/blog/2017/01/angular2-possible-parameters-for-hostbinding-and-hostlistener-decorators/feed/ 0