How can I identify which element generated 'ng-click' event?

22,621 views
Skip to first unread message

Sik

unread,
Sep 3, 2012, 8:34:33 AM9/3/12
to ang...@googlegroups.com
Hi, 

I'm a newbie on angularJS(and web developing).

Nowadays, I'm trying to make table ui with angularJS. 

And, I made the table with 'ng-repeat' directive for simple editing.(adding row becomes easy.)

************************************************************************
<tbody id= "table_row">
   <tr ng-repeat="row in rows" ng-click="tableRowClicked()">
        <td ng-repeat="col in row">{{col}}</td>
</tr>
************************************************************************

But, now I have a problem with above code.

Even if I set the ng-click on every rows,( For editing selected row.)

I can't identify which row was clicked.

Are there any way to identify which row was clicked?

Thank you for reading.

Regards.


Will Kriski

unread,
Sep 3, 2012, 9:01:01 AM9/3/12
to ang...@googlegroups.com
You can pass the $event object to the function and get the element from there or pass any data value from the row (eg id) in there - here's a fiddle I did http://jsfiddle.net/willkriski/yQs8z/59/

W

Peter Bacon Darwin

unread,
Sep 3, 2012, 9:54:36 AM9/3/12
to ang...@googlegroups.com
How about something like?
  <tr ng-repeat="row in rows" ng-click="tableRowClicked($index)"> 

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Jorge Sousa

unread,
Sep 3, 2012, 9:56:36 AM9/3/12
to ang...@googlegroups.com
Hi,
 
You could use tableRowClicked($index)  or tableRowClicked({{$index}})  (one is wrong!!!),
 
You could also use de current row object itselt.  tableRowClicked(row) 
 
Rows its your list model in this case (an Array), thereis probably something inside one of the rows that indentifes it.
 
Try to thing in terms of MODEL's  and not in terms of which element was clicked.
 
Jorge

Sik

unread,
Sep 4, 2012, 8:24:36 PM9/4/12
to ang...@googlegroups.com
Thank you! for all of your kind answer!

It's easily solved from your answers!

and, specifically, really thanks Will kriski! for showing handwriting jsfiddle code!

It helped a lot!.( I think that's an property of event in javascript- not a internal object of angularJs. is it right?)

Again, thank you very much!

ProLoser

unread,
Sep 4, 2012, 11:03:20 PM9/4/12
to ang...@googlegroups.com
Just a note: I would recommend Pete's solution over Will's (no offense will). You want to try and get your controllers as far abstracted as possible from the DOM. Leave that to your directives.

Stephen Nancekivell

unread,
Sep 6, 2012, 3:57:48 AM9/6/12
to ang...@googlegroups.com
How about  you make your tableRowClicked method take the row as a argument.
<tbody id= "table_row">
 
<tr ng-repeat="row in rows" ng-click="tableRowClicked(row)">

   
<td ng-repeat="col in row">{{col}}</td>
</tr>

Will Kriski

unread,
Sep 6, 2012, 6:47:10 AM9/6/12
to ang...@googlegroups.com
No problem, I just answered his original question :)

W

Reply all
Reply to author
Forward
0 new messages