Create array using ng-model in Angular.js

You need to use arrays to hold datas such as telephone numbers,addresses etc in your application. Lets see how to create an array using ng-model in Angular.js to store datas. If you want to store phone number in an array define $scope.telephone as an array in your controller.

$scope.telephone = [];

You have to use $complile Angular service to address the problem of ng-model not being recognised when you append a new input. $compile compiles an HTML string or DOM into a template and produces a template function,which can then be used to link scope and template together.

// Create input element

var input = angular.element('<div><input type="text" ng-model="telephone[' + $scope.inputCounter + ']"></div>');

// Compile the HTML and assign to scope

var compile = $compile(input)($scope);