当 DOM 完成渲染元素时执行一个方法
Execute a method when DOM finishes rendering the elements
在我的应用程序中,主页有一个从侧边菜单(另一个组件)接收 "options object" 的组件。
菜单有一个 "Publish" 按钮,当我按下它时。它通知我家observable
更新组件。
通知另一个 observable
(home) 它收到了更新的模型。
组件然后(通过 *ngIf*
)show/hide 相应地一些 DOM 元素。
问题是:
当获得 "back notification" I 运行 时,渲染元素上的 JavaScript 函数。但我不工作仅仅是因为它比 DOM 渲染先执行。
那么你们知道监听器的任何方法吗DOM?所以当它完成渲染时我可以调用函数?
如果它是一个 ngFor,我可以使用 (last) 变量上的指令让它工作。但事实并非如此...
我认为你可以使用 Angular 的生命周期钩子来解决这个问题。您可以浏览 documentation of angular lifecycle hooks 并根据需要使用它。
https://angular.io/guide/lifecycle-hooks
你需要的是
ngAfterViewInit()
Respond after Angular initializes the component's views and child
views / the view that a directive is in.
Called once after the first ngAfterContentChecked().
在我的应用程序中,主页有一个从侧边菜单(另一个组件)接收 "options object" 的组件。
菜单有一个 "Publish" 按钮,当我按下它时。它通知我家observable
更新组件。
通知另一个 observable
(home) 它收到了更新的模型。
组件然后(通过 *ngIf*
)show/hide 相应地一些 DOM 元素。
问题是: 当获得 "back notification" I 运行 时,渲染元素上的 JavaScript 函数。但我不工作仅仅是因为它比 DOM 渲染先执行。
那么你们知道监听器的任何方法吗DOM?所以当它完成渲染时我可以调用函数?
如果它是一个 ngFor,我可以使用 (last) 变量上的指令让它工作。但事实并非如此...
我认为你可以使用 Angular 的生命周期钩子来解决这个问题。您可以浏览 documentation of angular lifecycle hooks 并根据需要使用它。
https://angular.io/guide/lifecycle-hooks
你需要的是
ngAfterViewInit()
Respond after Angular initializes the component's views and child views / the view that a directive is in. Called once after the first ngAfterContentChecked().