Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  34] [ 7]  / answers: 1 / hits: 30818  / 10 Years ago, wed, march 12, 2014, 12:00:00

Is it possible to pass the HTMLElement to a ng-click configured on a controller?



Here's some sample code:



<div ng-controller=Controller>
<ul ng-repeat=item in items>
<li ng-click=handleThisElement($element) id={{item.id}} >{{item.name}}</li>
</ul>
</div>


Controller:



function ($scope) {
$scope.items = [
{name: 'Bilbo', id='Bilbo'},
{name, 'Frodo', id='Frodo'},
{name: 'Pippin', id='Pippin'},
{name: 'Merry', id='Merry'},
{name: 'Sam', id='Sam'}
];

$scope.handleThisElement = function (element) {
alert(element.id); // should alert (Bilbo || Frodo || Pippin || Merry || Sam)
}


UPDATE:



Don´t get confused, I said that I want get the whole element not only the id from the model.



$event.target - don't works in some versions of IE.


More From » angularjs

 Answers
45

The HTML:



<div ng-controller=Controller>
<ul ng-repeat=item in items>
<li ng-click=handleThisElement($event) id={{item.id}} >{{item.name}}</li>
</ul>
</div>


And the js:



function ($scope) {
$scope.items = [
{name: 'Bilbo', id='Bilbo'},
{name, 'Frodo', id='Frodo'},
{name: 'Pippin', id='Pippin'},
{name: 'Merry', id='Merry'},
{name: 'Sam', id='Sam'}
];

$scope.handleThisElement = function ($event) {
alert($event.target.id);
}

[#72032] Tuesday, March 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yamileth

Total Points: 53
Total Questions: 96
Total Answers: 112

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;