O'Reilly logo

Mastering Web Application Development with AngularJS by Pawel Kozlowski, Peter Bacon Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DOM event handlers

Our UI wouldn't be very useful if users couldn't interact with it (either by using a mouse, a keyboard or touch events). The good news is that registering event handlers is a child's play with AngularJS! Here is an example of reacting to a click event:

<button ng-click="clicked()">Click me!<button>

The clicked() expression is evaluated against a current $scope which makes it very easy to call any method defined on that scope. We are not limited to simple function calls; it is possible to use expressions of an arbitrary complexity, including ones that accept arguments:

<input ng-model="name">
<button ng-click="hello(name)">Say hello!<button>

Note

Developers new to AngularJS often try to register event handler as follows: ng-click="{{clicked()}}" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required