填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

HostBinding

一个装饰器,用于把一个 DOM 属性标记为绑定到宿主的属性,并提供配置元数据。 Angular 在变更检测期间会自动检查宿主属性绑定,如果这个绑定变化了,它就会更新该指令所在的宿主元素。

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.

选项说明
hostPropertyName

The DOM property that is bound to a data property.

选项

The DOM property that is bound to a data property.

      
      hostPropertyName: string
    

使用说明

下面的例子创建了一个指令,它会对具有 ngModel 指令的 DOM 元素设置 validinvalid 属性。

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