转载自 http://www.ngui.cc/news/show-126.html
在 Angular 中,我们可以使用 HostBinding 装饰器,实现元素的属性绑定。
指令的作用
该指令用于演示如何利用 HostBinding 装饰器,设置元素的 innerText 属性。
指令的实现
import { Directive, HostBinding} from '@angular/core';
@Directive({
selector: '[greet]' }) export class GreetDirective {
@HostBinding() innerText = 'Hello, Everyone!';
}
指令的应用
import { Component } from '@angular/core';
@Component({ selector: 'app-root',
template: `
<h2>Hello, Angular</h2>
<h2 greet>Hello, Angular</h2>
`,
})
export class AppComponent { }