ngClick directive and other event directives

The ngClick directive allows you to bind any custom behavior to the click event of the element.

The following code is an example of the usage of the ngClick directive calling a function.

<html ng-app="parking">
<script src="angular.js"></script>
var parking = angular.module("parking", []);
parking.controller("parkingCtrl", function($scope) {
$scope.appTitle = "Parking";
$ = [];
$scope.park = function(car) {
car.entrance = new Date();
delete $;
<body ng-controller="parkingCtrl">
<h3 ng-bind="appTitle"></h3>
<tr ng-repeat="car in cars">
<td><span ng-bind="car.plate"></span></td>
<td><span ng-bind="car.entrance"></span></td>
<input type="text" ng-model="car.plate" placeholder="What's the plate?" />
<button ng-click="park(car)">Park</button>

In the preceding code, there is another pitfall. Inside the ngClick directive, we will call the park function, passing car as a parameter. As long as we have access to the scope through the controller, it would not be easy if we just accessed it directly, without passing any parameter at all.

Other Event Directives
Other directives that have the same behavior but are triggered by other events are ngBlur, ngChange, ngCopy, ngCut, ngDblClick, ngFocus, ngKeyPress, ngKeyDown, ngKeyUp, ngMousedown, ngMouseenter, ngMouseleave, ngMousemove, ngMouseover,
, and ngPaste.
Related Tutorial
Follow Us #
Contents +