Component source
<aura:component> <aura:attribute name="setMeOnInit" type="String" default="default value" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <p>This value is set in the controller after the component initializes and before rendering.</p> <p><b>{!v.setMeOnInit}</b></p> </aura:component>
Client-side controller source
({ doInit: function(cmp) { // Set the attribute value. // You could also fire an event here instead. cmp.set("v.setMeOnInit", "controller init magic!"); } })
Let's look at the Component source to see how this works. The magic happens in this line.
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
This registers an init event handler for the component. init is a predefined event sent to every component. After the component is initialized, the doInit action is called in the component's controller. In this sample, the controller action sets an attribute value, but it could do something more interesting, such as firing an event.
Setting value="{!this}" marks this as a value event. You should always use this setting for an init event.