0

I am making my first steps with angular, I just want to simply clear the content of a input text when I click on it. this is the markup

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input id="123" type="text" ng-click="unsetValue($e)" ng-model="firstName"><br>
Last Name: <input id="456" type="text" ng-click="unsetValue($e)" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

this is the js in an external file

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";

$scope.unsetValue = function($event) {
    console.log($event);
}

when I click the control, $event is undefined, any idea why?

thanks M

user3174311
  • 1,714
  • 5
  • 28
  • 66
  • Note the correct syntax in the other post `$event` instead of `$e` and the answer on the linked post shows the directive source which shows the named `$event` – Nope Aug 30 '17 at 15:52

2 Answers2

0

You have to call ng-click in this way: ng-click="unsetValue($event)"

Update

Later in the function you can name parameter as you like for example:

$scope.unsetValue = function(e) {
    console.log(e);
}
Karol Trybulec
  • 1,056
  • 1
  • 11
  • 17
  • Thanks, I thought that was just a name for a variable, not that I had to use a specific name. I will accept this in 7 mins – user3174311 Aug 30 '17 at 15:50
0

The name of the variable passed in is $event, not $e

so you need to use ng-click like this

ng-click="unsetValue($event)"

the rest of your code is fine.

jperelli
  • 6,988
  • 5
  • 50
  • 85