0

My html:

<div id="contentDiv"> 
    <div id="headerDiv" ><div id="titleDiv"> Queries</div></div>
    <div id="valuesDiv" ><div id="yearDiv"> 2015</div></div>
    <div id="graphDiv" ><div id="chartDiv">graph</div></div>
</div>

Like this div, I have another div but the content in the div is different.

How to add a new div horizontally when I click on hyperlink using angularjs?

How can I do this? please help me out regarding this

1
  • Can you show the working demo to get the correct understanding. Commented Feb 3, 2016 at 11:40

1 Answer 1

0

Looks like what you need is a two way binding with the ng-model directive. So the idea would be that you bind the new div to a variable in your scope which is initially in an empty or undefined state (for example, there are better ways). When the hyperlink is clicked it calls the function specified by an ng-click directive which will fill your bound object, which in turn will cause the new div to be rendered.

EDIT: Based on your comments here is a simple example.

HTML page:

<div id="newDiv" ng-repeat="item in items">
 <!-- Div content -->
 <!-- example -->
 <input type="text" ng-model="item.name">
 </div>

 <input type="button" ng-click="addItem()">

Controller:

 $scope.items=[];
 $scope.addItem = function() {
    var newItem = {};
    newItem.name = "new item name";
    $scope.items.push(newItem);
 }

What's happening here is the data for each div is stored in an array of objects. The ng-repeat directive will repeat the div for each object in the array. You can then fill the elements in the div using the object. Adding a new div is as simple as adding a new item to the array and angular will take care of the rest for you. Please note that I have not tested this example, but hopefully it's enough to point you in the right direction.

RE aligning the divs horizontally, this will be done with CSS, using the inline-block display mode. So you could give the div a class of, for example, "horizontalDiv" and add the following class to your CSS file:

.horizontalDiv {
 display: inline-block;
}
Sign up to request clarification or add additional context in comments.

14 Comments

ok.Can u please edit the code when u get an angular environment
like this I have 2 more divs.For the first Onclick of an add button, should display a 1st div and for the second Onclick of an add button,should display a 2nd div and so on.How can I do this ?
ok, are all the divs the same, just with different data? Or is each div different?
all the divs are same but each div has different data and I should align all the divs horizontally
k sure.Waiting for ur reply
|

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.