0

Is there a way to access the div which is in the controller div or the controller defined div without defining them with and class or id JUST using the $scope.

<div ng-controller="gridController">
    <div></div>    // < --  I want to access this element 
</div>

To be a bit more specific does angular saves and gives access to the element DOM info which the ng-controller was called ?

6
  • What are you actually trying to do? There are many ways to do what you're describing. Commented Nov 25, 2014 at 0:09
  • I want to access the element.target of the first div or the pointed one Commented Nov 25, 2014 at 0:10
  • To be a bit more specific does angular saves and gives access to the element DOM info which the ng-controller was called ? Commented Nov 25, 2014 at 0:16
  • You shouldn't be accessing div's directly, it should be handled either via directive if you want to work with div as a DOM Commented Nov 25, 2014 at 0:21
  • I'm not sure if Angular exposes that information. However, I think it would be best if you explain what you're actually trying to do. Maybe then someone could suggest a solution to your problem (which may or may not involve trying to discover what element your controller directive lives on). Commented Nov 25, 2014 at 0:21

2 Answers 2

1

A controller has no concept of the DOM, and it should stay that way or you run the very likely risk of writing untestable code. This is a part of the separation of concerns in the angular framework. A controller can be bound to multiple different elements or even to the controller function of different directives and there would be know way to tell them apart.

If you are attempting to do anything to the DOM you should be using a directive.

Given more information about what you want to accomplish with the element in question more guidance to reach your goal could be given.

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

Comments

0

You can access the element the controller is defined on by injecting the angular variable $element into your controller

angular.module('myApp')
  .controller('SomeCtrl',['$scope', '$element', function($scope, $element) {

  $scope.buttonClick = function () {
    $element.find('div').css('background-color', '#f00');
  }
}]);

If jQuery is included in your project, $element will be a jQuery object of the element your controller is defined on (the outer div in your example). You can use standard jQuery directives to access its sub elements. Accessing an unnamed child div would require you to know its position, or use some other criteria to target it.

If jQuery is not installed, angular includes jqlite which is a subset of jQuery allowing you to use most jQuery selectors to target elements, but lacking most of the other manipulation features.

However, it is generally considered bad practice to access, and especially manipulate the DOM from within a controller. Because of the way jQuery binds variables to html, if you make changes to the DOM from certain functions, angular may not pick these changes up and overwrite them when it next does a binding cycle. To avoid this, you should do most of your DOM manipulation from inside an angular directive. Sometimes however, its just easier to do it from the controller... If you are going to do this, you should learn about $apply and $digest.

3 Comments

Thank you for the respond! But you will need to edit you answer and add the '$element' in the array of dependencies of the controller.
I tried the solution it worked fine but in the case which i call the controller through the routing provider I gives and unknown provider error do you have any idea how to handle that ?
We would probably need to see the code and this sounds like a separate question, but it could possibly be because in your edit you added the $element as part of the injected array, but didnt include the $scope variable. See my improved edit.

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.