I have the following AngularJs code to change the number of items based on the count.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("sub", [], function() {});
app.controller("my_ctrl", function($scope) {
$scope.range = function(n) {
return new Array(n);
};
$scope.submit = function() {
console.log($scope.config);
};
});
</script>
</head>
<body ng-app="sub" ng-controller="my_ctrl">
<form ng-submit="submit()">
item count:
<input type="number" min="0" max="10" ng-model="config.no_of_items"/>
<div ng-repeat="i in range(config.no_of_items) track by $index">
item name:
<input name="item" ng-model="config.items[$index]"/>
</div>
<input type="submit"></input>
<form>
</body>
It's working when I increase the count and add new items.
But when I decrease the count, the number of input boxes changes, but the element is not removed from the items array.
How do I achieve that?