{ "id": "guide/event-binding-concepts", "title": "How event binding works", "contents": "\n\n\n
In an event binding, Angular configures an event handler for the target event.\nYou can use event binding with your own custom events.
\nWhen the component or directive raises the event, the handler executes the template statement.\nThe template statement performs an action in response to the event.
\nA common way to handle events is to pass the event object, $event
, to the method handling the event.\nThe $event
object often contains information the method needs, such as a user's name or an image URL.
The target event determines the shape of the $event
object.\nIf the target event is a native DOM element event, then $event
is a DOM event object, with properties such as target
and target.value
.
In the following example the code sets the <input>
value
property by binding to the name
property.
With this example, the following actions occur:
\ninput
event of the <input>
element, which allows the code to listen for changes.input
event.$event
.getValue($event.target)
and updates the name
property.If the event belongs to a directive or component, $event
has the shape that the directive or component produces.
The type of $event.target
is only EventTarget
in the template.\nIn the getValue()
method, the target is cast to an HTMLInputElement
to allow type-safe access to its value
property.