Snippet 1:
- If you run this below code, then you can't add duplicate records.
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
$scope.products = ["1", "2", "3"];
$scope.addItem = function() {
$scope.products.push($scope.addMe);
}
});
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
</head>
<body>
<div ng-app="myShoppingList" ng-controller="myCtrl">
<ul>
<li ng-repeat="x in products">{{x}}</li>
</ul>
<input ng-model="addMe">
<button ng-click="addItem()">Add</button>
</div>
<p>Write in the input field to add items.</p>
</body>
</html>
And the above snippet throws an error for
Snippet 2:
- But you can add duplicate value on first time if you run this below snippet . But it's does not allow to add duplicate values while clicking on second time.
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
$scope.products = [1, 2, 3];
$scope.addItem = function() {
$scope.products.push($scope.addMe);
}
});
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
</head>
<body>
<div ng-app="myShoppingList" ng-controller="myCtrl">
<ul>
<li ng-repeat="x in products">{{x}}</li>
</ul>
<input ng-model="addMe">
<button ng-click="addItem()">Add</button>
</div>
<p>Write in the input field to add items.</p>
</body>
</html>
Snippet Code Difference :
Snippet 1 - $scope.products = ["1", "2", "3"];
// string values
Snippet 2 - $scope.products = [1, 2, 3];
// integer values
Question:
- Why angular does not allowing to add duplicate value on first time if the array values are
string
? - Why angular is allowing to add duplicate value on first time if the array values are
integer
- Why angular does not allow duplicate value on second time if the array values are
integer