I came across this code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
$scope.products = ["Milk", "Bread", "Cheese"];
// If I push a duplicate here, it will crash
});
</script>
<div ng-app="myShoppingList" ng-controller="myCtrl">
<ul>
<li ng-repeat="x in products">{{x}}<span ng-click="removeItem($index)">×</span></li>
</ul>
<input ng-model="addMe">
<button ng-click="addItem()">Add</button>
</div>
<p>Click the little x to remove an item from the shopping list.</p>
</body>
</html>
which runs OK. However, if I push a duplicate element in the array, like this:
$scope.products.push("Milk");
the application will crash. Why is that?
- Same behavior if I statically initialize an array with a duplicate.
- Sets are with curly brackets, so I don't get this.