This question already has an answer here:
这个问题在这里已有答案:
- Automatically pass $event with ng-click? 4 answers
- 使用ng-click自动传递$ event? 4个答案
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
我正在使用angular进行我的第一步,我只想在单击它时简单地清除输入文本的内容。这是标记
<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
这是外部文件中的js
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?
当我点击控件时,$ event未定义,任何想法为什么?
thanks M
谢谢M.
2 个解决方案
#1
0
You have to call ng-click
in this way: ng-click="unsetValue($event)"
您必须以这种方式调用ng-click: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);
}
#2
0
The name of the variable passed in is $event
, not $e
传入的变量的名称是$ event,而不是$ e
so you need to use ng-click
like this
所以你需要像这样使用ng-click
ng-click="unsetValue($event)"
the rest of your code is fine.
你的其余代码都没问题。
#1
0
You have to call ng-click
in this way: ng-click="unsetValue($event)"
您必须以这种方式调用ng-click: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);
}
#2
0
The name of the variable passed in is $event
, not $e
传入的变量的名称是$ event,而不是$ e
so you need to use ng-click
like this
所以你需要像这样使用ng-click
ng-click="unsetValue($event)"
the rest of your code is fine.
你的其余代码都没问题。