olivierGrech olivierGrech - 6 months ago 8
HTML Question

AngularJS - Repeating a div and a button

I want to use ng-repeat to repeat a div. This div also has a button within it.

Currently, I'm doing it by creating the div and the button in the javascript part and pushing the final result in an array :

var newDiv = document.createElement('div');
var newButton = document.createElement('button');
newDiv.appendChild(newButton);
$scope.arrayDiv.push(newDiv);


I have 2 questions :

1) What should be the html syntax ? Is the following correct ?

<div id='main_chart_div' ng-repeat="x in arrayDiv" value={{x}}></div>


2) Is there a way to do that without manipulating the DOM ?

T J T J
Answer

You can have the button in your template:

<div id='main_chart_div' ng-repeat="x in arrayDiv" value={{x}}>
 <button></button>
</div>

By the way, you shouldn't repeat an element with a static id.

Comments