Angular 5 - 指令中的生命周期挂钩

Angular 5 - lifecycle hook in directive

我是 Angular 5.
的新手 我已经为外部 JS 库创建了指令。
但是在同一指令中,我将值绑定到属性。

我正在尝试起诉 ngAfterViewInit 以检测是否所有值都绑定到该属性,然后调用 jQuery 插件。

但是我只找到了组件的生命周期钩子。我可以在指令中使用那些吗?这是一个不错的选择吗?

<div *ngFor="let item of easypiechartOptions"
    [option]="item"
    appEasyPieChart
    [attr.data-percent]="item.percent">
</div>

如果我不使用 ngAfterViewInit,那么当我调用 jQuery 插件时,值不会被绑定。
如果我使用它,当我调用 jQuery 插件时属性值就准备好了。

But I have find life cycle hook only for the component. Can I use those in directive? Is that a good choice?

我们用于组件的钩子似乎也用于指令。 我们可以从文档中理解这个概念,here.

(形成文档 :)

A directive has the same set of lifecycle hooks, minus the hooks that are specific to component content and views

Directive and component instances have a lifecycle as Angular creates, updates, and destroys them. Developers can tap into key moments in that lifecycle by implementing one or more of the lifecycle hook interfaces in the Angular core library

有一个在指令上使用熟悉的挂钩的示例 here

指令生命周期挂钩有助于 Ofig 的回答。将是:

  • ngOnInit
  • ngAfterContentCheckted
  • ngOnDestroy

Docs