javascript - Angular 5 - 指令中的生命周期 Hook

标签 javascript jquery directive angular5

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

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

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

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

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

最佳答案

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

我们为组件使用的钩子(Hook)似乎也适用于指令。 我们可以从文档中理解这个概念,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 上使用熟悉的钩子(Hook)的例子

关于javascript - Angular 5 - 指令中的生命周期 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48734738/

相关文章:

javascript - 两个脚本将不同的监听器添加到同一元素

javascript - ngClass 不应用父指令中的更改

javascript - 检测 HTTP 或 HTTPS,然后在 JavaScript 中强制使用 HTTPS

jQuery - :not()? 中的多个选择器

javascript - 如何触发使用 Ajax 动态创建的元素上的指令?

angularjs - AngularJS $ watch改变元素的宽度

AngularJS 将 $resource 作为指令参数传递

javascript - 当调用来自 iFrame 时,如何获取主机站点的 Http_Referrer?

JavaScript var 在 Rails 中无法初始化带有特殊字符的长文本

angularjs - 检查元素是否已经有监听器