Please check this Fiddle: http://jsfiddle.net/kgXRa/
Here is the code (Implemented in the JSFiddle)
var app = angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', function ($scope) {
$scope.field = [];
$scope.value = [];
$scope.inputCounter = 0;
}]);
app.directive('addInput', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.find('button').bind('click', function () {
var input = angular.element('<div><input type="text" ng-model="field[' + scope.inputCounter + ']"><input type="text" ng-model="value[' + scope.inputCounter + ']"> <a href="javascript:;" ng-click="remove_it(' + scope.inputCounter + ')">remove</a></div> ');
var compile = $compile(input)(scope);
element.append(input);
scope.inputCounter++;
scope.remove_it = function(scope_counter) {
compile.remove(this);
scope.inputCounter--;
}
});
}
}
}]);
I need to add the remove button when user creates the new fields. Somehow I need to drop the counter so the Array is cleared up as user delete the input. I have tried to use jQuery however I think there should be a way in Angular.JS
With the script I wrote it remove the wrong one.
Thanks guys.