Help Angular by taking a 1 minute survey!Go to surveyHome

HostBinding

Decorator that marks a DOM property as a host-binding property and supplies configuration metadata. Angular automatically checks host property bindings during change detection, and if a binding changes it updates the host element of the directive.

OptionDescription
hostPropertyName

The DOM property that is bound to a data property.

Options

The DOM property that is bound to a data property.

      
      hostPropertyName: string
    

Usage notes

The following example creates a directive that sets the valid and invalid properties on the DOM element that has an ngModel directive on it.

@Directive({selector: '[ngModel]'}) class NgModelStatus { constructor(public control: NgModel) {} @HostBinding('class.valid') get valid() { return this.control.valid; } @HostBinding('class.invalid') get invalid() { return this.control.invalid; } } @Component({ selector: 'app', template: `<input [(ngModel)]="prop">`, }) class App { prop; }
      
      
  1. @Directive({selector: '[ngModel]'})
  2. class NgModelStatus {
  3. constructor(public control: NgModel) {}
  4. @HostBinding('class.valid') get valid() { return this.control.valid; }
  5. @HostBinding('class.invalid') get invalid() { return this.control.invalid; }
  6. }
  7.  
  8. @Component({
  9. selector: 'app',
  10. template: `<input [(ngModel)]="prop">`,
  11. })
  12. class App {
  13. prop;
  14. }