1

I was doing some tests with angular.element and wanted to figure out if there is a way to get the current DOM from an hg-click?

Example

<div ng-click="angular.element(curElement).parent()..."></div>

How do I get the current element? I.e the Div element it belongs to? I don't need to use a directive or a controller, I just want to figure out if there is a way through the above method.

I tried passing in 'this' and '$element' but nothing.

4
  • 1
    What happens if you console.log angular.element(this)? Commented Sep 8, 2014 at 14:28
  • i don't understand the use case...you don't need a controller or directive? best-practice-wise, you shouldn't really be doing that. Commented Sep 8, 2014 at 14:40
  • I have controllers and directives but again, I don't want to get too much into the specifics, just needed a way to do something similar to above. I am aware of the best practices. Commented Sep 8, 2014 at 15:00
  • for the console.log, I get a $$childScope object: $$childScopeClass {$$childTail: null, $$childHead: null, $$nextSibling: $$childScopeClass, $$watchers: ...} Commented Sep 8, 2014 at 18:13

2 Answers 2

2

You need to use the $event service as parameter

 <button ng-click="clickMe($event)">Click Me</button>

Online Demo

There are several properties that can help:

srcElement, toElement or target

Sign up to request clarification or add additional context in comments.

2 Comments

Great, this work and I can get the element using target. Do you know if it is possible to execute the 'angular.element' expression within an hg-click directly without being wrapped in a controller function? Just a curiosity.
Out of curiosity what are they?
1

If you define a function so that

<div ng-click="doStuff($event)"></div>

Somewhere in your controller, you can then get a reference to the raw DOM element by using

$scope.doStuff = function ($event){
    var el = $event.target;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.