I am developing an Web Application using Angular JS. I am a beginner to AngularJS. In my app, I need to dynamically add and remove elements. I use directive for it. Adding and removing elements is working fine. But I am having a problem with retrieving each model value of added inputs.
My scenario is below.
This is my app
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Angularjs popup</title> <link href="http://localhost:8888/angular/bootstrap.css" rel="stylesheet"> <script src="http://localhost:8888/angular/angular-js.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller='myCtrl'> <div> <button class="btn btn-success" ng-click="addRow()">Add row</button> <hr /> <div id="rowsContainer"> </div> <button class="btn btn-primary" ng-click="submitForm()">Submit</button> </div> </div> </body> <script type="text/javascript"> var app = angular.module('myApp',[]); app.controller('myCtrl',function($rootScope,$scope,$compile){ $rootScope.getContacts = function() { var list = [{contactId: 1,contactType: 'Mobile'} , {contactId: 2,contactType: 'Office'} , {contactId: 3,contactType: 'Home'}]; return list; } $scope.contactType = []; $scope.contactValue = []; $scope.submitForm = function(){ alert($scope.contactType) //I want to retrieve ContactValue and ContactType here. All values by looping throgh }; $scope.addRow = function() { var divElement = angular.element(document.querySelector('#rowsContainer')); var appendHtml = $compile('<dynamic-Row contact-type="contactType"></dynamic-Row>')($scope); divElement.append(appendHtml); } }); app.directive('dynamicRow', function() { return { restrict: "E", scope: { contactType: "=contactType", contactValue : "=contactValue" }, templateUrl:'http://localhost:8888/angular/dynamic/row.html', controller: function($rootScope, $scope, $element) { $scope.contacts = $rootScope.getContacts(); $s cope.contactType.push($scope.contact_type) $scope.contactValue.push($scope.contact_value) $scope.deleteRow = function(e){ $element.remove(); $scope.$destroy(); } } } }); </script> </html>
I commented what I want to do in the above code.
this is the row.html
<div class="row"> <div class="col-md-5"> <select name="ContactType" class="form-control" ng-model="contact_type" > <option ng-repeat="contact in contacts" value="{{contact.contactId}}"> {{contact.contactType}} </option> </select> </div> <div class="col-md-5"> <input ng-model="contact_value" class="form-control"> </div> <div class="col-md-2"> <button ng-click="deleteRow($event)" class="btn btn-danger">X</button> </div> </div>
How can I get the each value of all dynamically added input controls in submitForm function?