Pallavi Sharma
Pallavi Sharma

Reputation: 655

how to add row dynamically in angular .js on button click?

I am creating a list view in angular.js when I take static data I am able to create list .as In given fiddle.http://jsfiddle.net/65Cxv/50/.But I need to generate row dynamically in other word I need to create row when user click button.I need to create list having same text Example ("List") but different ID like ("0","1","2"...etc).can it is possible to generate a list ..?here I am trying to do ..

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="//code.jquery.com/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<button type="button" class="btn btn-primary">Primary</button>
<ul ng-controller="ListController">
    <li>
        <a ng-click=></a>
    </li>
</ul>



</body>

</html>

JS Code:

var myApp = angular.module('myApp',[]);

myApp.controller('ListController', function($scope) {
  alert('--')
]);

Upvotes: 1

Views: 1627

Answers (1)

charlietfl
charlietfl

Reputation: 171690

You add to the data model by pushing new objects to your data array.:

$scope.items = [
    {name: 'item1', content: 'content1'},
    {name: 'item2', content: 'content2'},
    {name: 'item3', content: 'content3'}
];
/* bind this to `ng-model` of 2 inputs in html*/
$scope.activeItem={ name: '', content:''}

$scope.addItem=function(){
   $scope.items.push( $scope.activeItem);
   $scope.activeItem={} /* reset active item*/

}

In html use

<button ng-click="addItem()">Add</button>

DEMO

Upvotes: 1

Related Questions