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; }
      
      @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;
}