Why angularjs(ng-repeat) allowing to add duplicate record at first time if the input array values are integer types

Ramesh Rajendran

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

"[ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: x in products, Duplicate key: string:1, Duplicate value:


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
NTP

It is because by default input type is text therefore string, so number 2 is not equel to string "2"

if you add type number to your input

<input type="number" ng-model="addMe"> 

in your 2nd example you would see that it will not allow duplicate entry

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 type="number" ng-model="addMe">
      <button ng-click="addItem()">Add</button>
    </div>
    <p>Write in the input field to add items.</p>
  </body>
</html>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

not allowing duplicate values of a class when creating a constructor from user inputs

分類Dev

Repeat Values in array and shuffle it

分類Dev

For each duplicate in array, find the index of the first duplicate

分類Dev

動的json / objectでangularjsng-repeat

分類Dev

Count duplicate values in hash of an array

分類Dev

Why are flexible types not allowed in record type definitions?

分類Dev

SQL:How to select the first record from duplicate rows?

分類Dev

SQL:How to select the first record from duplicate rows?

分類Dev

Add inputs values in array

分類Dev

How to create not repeat pairs using option input types and jQuery + UnderscoreJS

分類Dev

FSharp Record Types With Entity Framework Code-First

分類Dev

How can I add the value if there are duplicate record in a list of dictionaries?

分類Dev

Reading from an Input File to an Integer Array

分類Dev

How to remove duplicate values from an array in PHP

分類Dev

How to remove duplicate values from an array in PHP

分類Dev

Remove duplicate values from Array of Dictionaries

分類Dev

best way to group adjacent duplicate values into an array

分類Dev

count duplicate values in array using angularjs

分類Dev

ng-repeat内のAngularJSng-option

分類Dev

THのAngularJSng-repeatグループ

分類Dev

ng-repeat内のAngularJSng-form

分類Dev

AngularJSng-repeatの問題-空白の結果

分類Dev

AngularJSng-repeatが機能しない

分類Dev

AngularJSng-repeatが機能しない

分類Dev

last record of php (json) on an array then add 1

分類Dev

How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

分類Dev

Why is first gets() input is lost in this c program?

分類Dev

How to add additional values in to array

分類Dev

Why transparency rgba works only first time

Related 関連記事

  1. 1

    not allowing duplicate values of a class when creating a constructor from user inputs

  2. 2

    Repeat Values in array and shuffle it

  3. 3

    For each duplicate in array, find the index of the first duplicate

  4. 4

    動的json / objectでangularjsng-repeat

  5. 5

    Count duplicate values in hash of an array

  6. 6

    Why are flexible types not allowed in record type definitions?

  7. 7

    SQL:How to select the first record from duplicate rows?

  8. 8

    SQL:How to select the first record from duplicate rows?

  9. 9

    Add inputs values in array

  10. 10

    How to create not repeat pairs using option input types and jQuery + UnderscoreJS

  11. 11

    FSharp Record Types With Entity Framework Code-First

  12. 12

    How can I add the value if there are duplicate record in a list of dictionaries?

  13. 13

    Reading from an Input File to an Integer Array

  14. 14

    How to remove duplicate values from an array in PHP

  15. 15

    How to remove duplicate values from an array in PHP

  16. 16

    Remove duplicate values from Array of Dictionaries

  17. 17

    best way to group adjacent duplicate values into an array

  18. 18

    count duplicate values in array using angularjs

  19. 19

    ng-repeat内のAngularJSng-option

  20. 20

    THのAngularJSng-repeatグループ

  21. 21

    ng-repeat内のAngularJSng-form

  22. 22

    AngularJSng-repeatの問題-空白の結果

  23. 23

    AngularJSng-repeatが機能しない

  24. 24

    AngularJSng-repeatが機能しない

  25. 25

    last record of php (json) on an array then add 1

  26. 26

    How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

  27. 27

    Why is first gets() input is lost in this c program?

  28. 28

    How to add additional values in to array

  29. 29

    Why transparency rgba works only first time

ホットタグ

アーカイブ