【发布时间】:2018-11-12 13:55:55
【问题描述】:
Angular documentation 声明 AfterContent 和 AfterView 生命周期挂钩仅适用于组件(而不是指令)。尽管如此,我有一个指令显然可以毫无问题地使用它们。
在指令中使用这些钩子有哪些限制/可能的问题?
这是一个使用 AfterContentInit 的指令示例:
@Directive({
selector: '[myDirective]'
})
export class MyDirective implements AfterContentInit {
ngAfterContentInit() {
console.log('my directive after content init');
}
}
@Component({
selector: 'my-app',
template: `
<div myDirective></div>
`,
})
export class App {}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, MyDirective ],
bootstrap: [ App ]
})
export class AppModule {}
【问题讨论】:
标签: angular