4

I am generating a list as below

<div ng-repeat="list in fileUploadList">
    <div id="{{list}}Upload"></div>
</div>

Inside the controller I have to get the element by ID so I am using this line:

document.getElementById($scope.fileUploadList[0] + 'Upload')

I logged the result to the console but it is returning null. What should I do?

Function in controller

$scope.loadHandleImage = function () {

     console.log(document.getElementsById($scope.fileUploadList[0] + 'Upload'));
            for (var i = 0; i < $scope.fileUploadList.length; i++)
            {  
                document.getElementById($scope.fileUploadList[i] + 'Upload').addEventListener('change', handleImage, false);   
            }

where handleImage is a function to be called

3
  • try maybe angular.element('#' + $scope.fileUploadList[0] + 'Upload');, what is list? is it just a string? with spaces? Commented Jul 7, 2016 at 6:31
  • The controller should have no knowledge of your DOM. If you are calling document from within your controller you are not understanding the Angular separation of concerns design patterns. Commented Jul 7, 2016 at 6:45
  • list is returning a string Commented Jul 7, 2016 at 6:49

6 Answers 6

1

I have got the problem document was not ready when you were trying to access the element use angular.element(document).ready method

angular.element(document).ready(function() {
console.log(document.getElementById($scope.fileUploadList[0] + "Upload"))
});

here is working example http://jsfiddle.net/hqqewnyc/

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

1 Comment

I checked using debugger. Its assiging the IDs
0

instead of ids use a classname on the elements then get elements by class name - this will give you some kind of array you can then loop through and get whatever attribute you want off each element.

Comments

0

Use

angular.element('#' + $scope.fileUploadList[0] + 'Upload').on('change', handleImage)

instead of document.getElementById and addEventListener

Comments

0

A simple way would be (if you dont care about your list object not being in the random id)

<div ng-repeat="list in fileUploadList" >
  <div id="{{$index}}Upload" ></div>
</div>

Then in the controller get the id by

document.getElementById('0Upload')
document.getElementById('1Upload')

..etc

2 Comments

How would I know the exact number of elements the array is returning.
That would be the fileUploadList.length
0

Not angular expert but based on my DOM experience there are ways to get ID attribute. The way i do in normal javascript is here

str = "";
for(tmp=0;tmp<10;tmp++)
{
    str = str + "<div id='myid" + tmp + "' onclick='idofobject(\"myid" + id + "\")'>Click Me</div>";
}

i created html with DIV and dynamically assign it a id like myid1 myid2 etc. there is a function call for each div idofobject that takes the id

just a hint that there can be ways to get dynamic id of objects

Comments

0

As each div gets dynamic ID, so the dynamic elements should be fetched using parent child relation. EX: If you add 2 button in a div dynamically fetch the first and second child of that div to get each button.

2 Comments

You can see in my given code I am calling them dynamically.. :)
Please see the below thread you need to get the child elements using the outer div , you should give the outside div a id and they get child for that id: stackoverflow.com/questions/19500592/…

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.