⚠️ This lesson is retired and might contain outdated information.

Control Angular 2 Events with $event and Event Handlers

Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 2 years ago

This lesson talks about the benefits of using the parens-based (click) syntax so that Angular 2 can handle any custom event. Then the video explains how to pass in the actually event object using the familiar $event syntax.

[00:00] If you need to get the actual MouseEvent, you can use the "$Event" syntax, which is just the same way we did it in Angular 1, so that the Event will be passed along. Now this value is the actual MouseEvent, so now when I click on this button, you'll see MouseEvent with all the properties you would expect from the MouseEvent.

[00:19] After that Event, you can go ahead and type ", myInput.value" and as well say that this is the Event, and we want to log out the Event, and then the value, so the "$Event" is here, the input value is here, and we're logging out the Event and the value. Now when we click and I type something, "Hello," and click, you'll see the MouseEvent and the value of "Hello."

[00:45] This will work on any DOM Event. If you were to say "mouseover" and hit save, now instead of onclick, every time I mouse over this button, you'll see those MouseEvents firing, and the value "Hello" of the form logging out as well.