Is there a way to select a DOM element (i.e. to replicate $('selector') of jQuery) - in AngularJS using angular.element?
-
4What precisely are you trying to do? The magic of angular is that you generally don't have to do that kinda thingZach Lysobey– Zach Lysobey2014-04-29 15:11:58 +00:00Commented Apr 29, 2014 at 15:11
-
I need to access an element which has $index for data in $scope from another element.Venkat– Venkat2014-04-29 15:18:24 +00:00Commented 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.btm1– btm12014-04-29 15:19:38 +00:00Commented Apr 29, 2014 at 15:19
-
Thanks for you reply.. and i should really think in angular way as you said.Venkat– Venkat2014-04-29 15:40:44 +00:00Commented Apr 29, 2014 at 15:40
1 Answer
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
7 Comments
require: '^myTabs', in a directive instead of trying a way to select the element?$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...$index in... ng-click="doSomethingWith($index)" or you can set a $watch function: $scope.$watch('data', function() { ...