Lifecycle Methods and Hooks

If your controller has any of these methods, they will be called during specific moments during the lifecycle of your controller.

onInit() is called after the custom element is connected to a DOM. It's a good place to set up your initial properties. Child elements are not yet attached to the DOM, templates are not parsed, and most other things are not yet set up.

onParse() is fired when all child nodes are parsed for light DOM elements and is fired immediately after onInit() for shadow DOM elements. If you are doing your own light DOM content projection, this would be an important method to use. With lengthy HTML, there might be multiple chunks of child nodes added as children to the custom element. This method is called after all of them are added, using a technique of watching for the document's content to be loaded and using a mutation observer to see if any parents have next siblings.

onViewInit() will be executed at the very end after the custom element is connected to the DOM and the template has also been attached and processed. At this point, all children are in the DOM, #ref directives are set up, and your element is basically live.

onChange(propertyName, oldValue, newValue) will be triggered whenever a monitored property is updated. Monitored properties are the ones that are referenced in a template, plus any attribute or property listed in the config. Differences are detected by using === (strict equals), so changes to immutable objects trigger change detection more easily.

Calling update() (see Utilities) will also call onChange() for all attributes and properties that are copied into the controller.

onDestroy() is when the controller is being detached from the custom element and the custom element is being disconnected from the DOM.