且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的组件绑定在其控制器中未定义?

更新时间:2023-02-12 11:27:39

使用angular的组件时,控制器尚未通过内部链接连接。如果您尝试在控制器的构造函数中执行此操作,则尚未将链接绑定到绑定。 Component API公开了一些你可以定义的生命周期钩子,它会在特定时间触发。你正在寻找 $ onInit 钩子。

When using angular's components, there is a point where the controller hasn't been wired up via the internal linking. If you're trying to do this in the constructor of your controller, you haven't been linked to the bindings. The Component API exposes a few life-cycle hooks that you can define that will fire at certain times. You're looking for the $onInit hook.


$ onInit() - 在构造了元素上的所有控制器并初始化它们的绑定之后(以及在该元素上的指令的前置和后置链接函数之前),在每个控制器上调用。这是为您的控制器放置初始化代码的好地方。

$onInit() - Called on each controller after all the controllers on an element have been constructed and had their bindings initialized (and before the pre & post linking functions for the directives on this element). This is a good place to put initialization code for your controller.

每个文档 - https://docs.angularjs.org/guide/component