Accessing elements in a directive template


I have a directive with an external template file. I am trying to get a particular HTML element, for each directive, and modify it. Is this possible?

This directive will go into a ng-repeat, so trying to access it using document.getElementById() only yields the first element(obviously). using document.getElementByClassName(), in a directive seems wasteful (haven’t tried it though).

You can try document.querySelectorAll.

But that would retrieve ALL the elements matching the selector. That would be wasteful, since i want to access it from the directive itself. Like when i use the directive in a ng-repeat. There’s got to be some other way?

I’m sorry, I misunderstood you. I thought you wanted to select more elements. If you just need one, you can use document.querySelector

but thats a little bit hacky and inefficient.
Why are you not building a direktive which hangs on exact the element you need?

please try to explain what you want to do… because using native javascript selector function destroys the intention of angularjs ^^

I’m trying to get the directives in a list (ng-repeat), and set the properties of certain elements, in the directive, DOM style.

But that will fetch only the first element matching the criteria.

Guys, i’m sorry if i’m not clear enough.

Let me explain again:
consider a directive dirA.
it is referencing a templateUrl containing (example):

<div id="an_id">

i’m modifying the div, in the directive’s controller, or link function:

.directive('dirA', function() {
    return {
                  controller: function() {
                       var element = document.getElementById('an_id');
              = "100px";
              = "100px";
                  templateUrl: "template.html"

Then, i use this directive with an ng-repeat:

<div ng-repeat="item in items">

obviously, only the first div is set, since id is unique and getElementById retrives one element.

is it possible to get the elements using this in the directive?

look into the replace option for direcitve definitions… so you can replace the directive dom-node with the content of the template… so element would be your div.

or use the children selector of jqlite:

add the content of your referenced template to your ng-repeat directly or with ngInclude and add your directive to the dom not you need.

A directive should be placed on the dom-node you need and try not to make ugly handlings at parent nodes

@alterecho, I would do something like this:

yeah but thats totally not how you should do it…

if you want to change elements… write a directive that is working directly on the elements you need

What you are doing is putting directive inside a template and including the template in ng-repeat and you think this is a better way?
I agree to disagree :).

i wanted to keep the wanted structure “alive”… i would never include such a simple template ^^.

if the template is reused somewhere else or is complex i would use it yep.
because loading the template 1 time and use for the other repeats the cached one ;).

so i only wanted to show how to use directives

Thanks guys. I have trouble if i try this:

link: function(scope, element, attribs) {
                var children = element.children();
                for (i in children) {
                    elem = children[0];     //0 here, since there is some other object retrieved too.
                    console.log("id: ",;      //logs correctly as "an_id"
                    console.log(elem);    //outputs <div style="" id="an_id">
           = = "purple";
                    console.log(elem);    //outputs <div style="" id="an_id">


Why is the style being cleared?? If i don’t remove the line it logs the correct style.

I would not recommend changing the styles directly on element or its children using JS. The better way is to add the CSS class on root element, like in the codepen before (instead of id=‘and_id’) and than use CSS to style individual children, like this:

.main {

.main div {

.main div p {

But what if i want to get a particular tag, and change it’s image?