I am new to angular and stuck with dynamic data binding. I have four data fields 2 static and 2 dynamically adding after add button. When I click add button only two of the fields are shown on the display and remaining data is not populated. I might had make some mistake in data passing. The first two fields are static and second two are dynamic need to add as user clicks. The same happens when using form. Will any one please help me resolve this. Thanks in advance. Below is my code:
Controller:var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.items = [];
$scope.itemsToAdd = [{
firstName: '',
lastName: ''
}];
$scope.add = function(itemToAdd) {
var index = $scope.itemsToAdd.indexOf(itemToAdd);
$scope.itemsToAdd.splice(index, 1);
$scope.items.push(angular.copy(itemToAdd))
}
$scope.addNew = function() {
$scope.itemsToAdd.push({
firstName: '',
lastName: ''
})
}
});
View (HTML):
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="plunker" ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div ng-repeat="item in items">
{{item.regno}} {{item.section}}
{{item.firstName}} {{item.lastName}}
</div>
<input type="text" ng-model="itemToAdd.regno" />
<input type="text" ng-model="itemToAdd.section" />
<div ng-repeat="itemToAdd in itemsToAdd">
<input type="text" ng-model="itemToAdd.firstName" />
<input type="text" ng-model="itemToAdd.lastName" />
<button ng-click="add(itemToAdd)">Add</button>
</div>
<div>
<button ng-click="addNew()">Add new</button>
</div>
</body>