1

Is there a way to select a DOM element (i.e. to replicate $('selector') of jQuery) - in AngularJS using angular.element?

4
  • 4
    What precisely are you trying to do? The magic of angular is that you generally don't have to do that kinda thing Commented Apr 29, 2014 at 15:11
  • I need to access an element which has $index for data in $scope from another element. Commented Apr 29, 2014 at 15:18
  • I think you are using angular incorrectly and need to read the documentation and how to think in angular. Commented Apr 29, 2014 at 15:19
  • Thanks for you reply.. and i should really think in angular way as you said. Commented Apr 29, 2014 at 15:40

1 Answer 1

4

The documentation for angular.element seems pretty clear to me in that it repeatedly says "does not support selectors". On the other hand, if you've included jQuery before the DOMContentLoaded event, then it would support selectors because it is jQuery.

However, if you're trying to select a random element from the DOM you should re-think your approach, as it is defeating the purpose of using something such as Angular.

If you're just trying to get a reference to the element on which a directive is running, then you can get that element as an argument to a function you specify as link. See the section titled "Creating a Directive that Manipulates the DOM" here: https://docs.angularjs.org/guide/directive

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

7 Comments

ng-click="derp()" is certainly the 'more angular way' for instances where you need to operate on an element within the scope of the current controller. jqLite provides support for lots of sub element selections as well docs.angularjs.org/api/ng/function/angular.element
Thanks for your reply.. And should i use require: '^myTabs', in a directive instead of trying a way to select the element?
Can you be a little more clear about what it is you are trying to accomplish?
i am about to save some content lets say $scope.data which is an array and i have ng-repeat-ed it and i have an button to save it. and i am using ng-click directive to access a function to save. now i need to know which data has been changed/modified in $scope.data as my save button dont know on which $index i am modifying...
well you can pass $index in... ng-click="doSomethingWith($index)" or you can set a $watch function: $scope.$watch('data', function() { ...
|

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.