0

I am newbie to AngularJs. I need to create set of div blocks (given as code) on button click.

<div>
     <div>Div A</div>
     <div>Div B</div>
     <div>Div C</div>
</div>

I have done this using hard coded divs Demo. But I just need to use ng-repeat and directives. Any help will be appreciated.

2
  • Hard coded divs ? From input ? More info please.. Commented Apr 23, 2014 at 14:13
  • no divs cannot be hard coded. it needs to be created from templates Commented Apr 23, 2014 at 14:24

1 Answer 1

1

Using ng-click is the way to go for binding the click event to your button. You should not be trying to handle button clicks with a directive. The demo you have is very close to what you need. There is a working Plunk HERE, but the general guts of it follow:

<button ng-click="myFunction()">Add</button>
<div ng-repeat="item in myList">
    <div>{{item.A}}</div>
    <div>{{item.B}}</div>
    <div>{{item.C}}</div>
</div>

$scope.myList = [];
$scope.myFunction = function(){
    var myItem = {A:someValue, B:someOther, C:someThing};
    $scope.myList.push(myItem);
};

The divs that hold the items could also make use of directives to change them somehow, but that is quite a bit more code. There are plenty of SO answers and Angular documentation that show you how to write directives, so I won't repeat them here.

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

1 Comment

Really, i dont understand your point. Can you give me any sample?

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.