Angular ng-repeat Error "duplikaty w repeaterze nie są dozwolone."

Definiuję filtr niestandardowy w ten sposób:

<div class="idea item" ng-repeat="item in items" isoatom>    
    <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
        ....
    </div>
</div>

Jak widać ng-repeat gdzie używany jest filtr jest zagnieżdżony w innym ng-repeat

Filtr jest zdefiniowany następująco:

myapp.filter('range', function() {
    return function(input, min, max) {
        min = parseInt(min); //Make string input int
        max = parseInt(max);
        for (var i=min; i<max; i++)
            input.push(i);
        return input;
    };
});

I ' m getting:

Błąd: duplikaty w repeaterze są niedozwolone. Repeater: komentarz w pozycji.komentarze / przedział: 1: 2 ngRepeatAction@ https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an

Author: Félix Gagnon-Grenier, 2013-04-30

10 answers

Rozwiązanie jest właściwie opisane tutaj: http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/

AngularJS nie zezwala na duplikaty w dyrektywie ng-repeat. Oznacza to, że jeśli próbujesz wykonać następujące czynności, otrzymasz błąd.

// This code throws the error "Duplicates in a repeater are not allowed.
// Repeater: row in [1,1,1] key: number:1"
<div ng-repeat="row in [1,1,1]">

Jednak nieznaczna zmiana powyższego kodu w celu zdefiniowania indeksu w celu określenia wyjątkowości, jak poniżej, spowoduje jego ponowne działanie.

// This will work
<div ng-repeat="row in [1,1,1] track by $index">

Oficjalne dokumenty są tutaj: https://docs.angularjs.org/error/ngRepeat/dupes

 960
Author: Webnet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-24 19:33:49

Dla tych, którzy oczekują JSON i wciąż otrzymują ten sam błąd, upewnij się, że analizujesz swoje dane:

$scope.customers = JSON.parse(data)
 46
Author: usefulBee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-25 21:32:58

Miałem problem w moim projekcie, w którym używałem ng-repeat track by $index, ale produkty nie odbijały się, gdy dane pochodzą z bazy danych. Mój kod jest jak poniżej:

<div ng-repeat="product in productList.productList track by $index">
  <product info="product"></product>
 </div>

W powyższym kodzie produkt jest odrębną dyrektywą do wyświetlania produktu.Ale dowiedziałem się, że $index powoduje problem, gdy przekazujemy dane z zakresu. Tak więc straty danych i DOM nie mogą być aktualizowane.

Znalazłem rozwiązanie używając product.id jako klucz w NG-powtórz jak poniżej:

<div ng-repeat="product in productList.productList track by product.id">
  <product info="product"></product>
 </div>

Ale powyższy kod ponownie zawodzi i wyrzuca poniższy błąd, gdy więcej niż jeden produkt ma ten sam id:

Kanciaste.js: 11706 błąd: [ngRepeat:dupes] duplikaty w repeaterze nie są dozwolone. Użyj wyrażenia "śledź według", aby określić unikalne klucze. Repeater

Więc w końcu rozwiązałem problem, wykonując dynamiczny unikalny klucz ng-repeat jak poniżej:

<div ng-repeat="product in productList.productList track by (product.id + $index)">
  <product info="product"></product>
 </div>
To rozwiązało mój problem i mam nadzieję, że to ci pomoże w przyszłości.
 12
Author: Mahima Agrawal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Co zamierzasz zrobić z filtrem" range"?

Oto próbka robocza tego, co myślę próbujesz zrobić: http://jsfiddle.net/evictor/hz4Ep/

HTML:

<div ng-app="manyminds" ng-controller="MainCtrl">
  <div class="idea item" ng-repeat="item in items" isoatom>    
    Item {{$index}}
    <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
      Comment {{$index}}
      {{comment}}
    </div>
  </div>
</div>

JS:

angular.module('manyminds', [], function() {}).filter('range', function() {
    return function(input, min, max) {
        var range = [];
        min = parseInt(min); //Make string input int
        max = parseInt(max);
        for (var i=min; i<=max; i++)
            input[i] && range.push(input[i]);
        return range;
    };
});

function MainCtrl($scope)
{
    $scope.items = [
        {
            comments: [
                'comment 0 in item 0',
                'comment 1 in item 0'
            ]
        },
        {
            comments: [
                'comment 0 in item 1',
                'comment 1 in item 1',
                'comment 2 in item 1',
                'comment 3 in item 1'
            ]
        }
    ];
}
 6
Author: Ezekiel Victor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-30 09:54:59

Jeśli przypadkiem ten błąd wystąpi podczas pracy z SharePoint 2010: Zmień nazwę swojego .rozszerzenia plików json i pamiętaj, aby zaktualizować ścieżkę restService. Nie było wymagane dodatkowe "track by $index".

Na szczęście Przekierowano mi ten link do tego uzasadnienia:

.json staje się ważnym typem pliku w SP2010. SP2010 zawiera punkty końcowe certains webservice. Lokalizacja tych plików to folder 14hive \ isapi. Rozszerzeniem tych plików są .json. To jest powód daje taki błąd.

"dba tylko o to, aby zawartość pliku json była json-a nie jego rozszerzeniem"

Po zmianie rozszerzeń plików należy ustawić wszystkie.

 1
Author: CR Rollyson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-29 16:28:31

Na wypadek, gdyby przydarzyło się to komuś innemu, dokumentuję to tutaj, otrzymywałem ten błąd, ponieważ omyłkowo ustawiłem model ng taki sam, jak tablica ng-repeat:

 <select ng-model="list_views">
     <option ng-selected="{{view == config.list_view}}"
         ng-repeat="view in list_views"
         value="{{view}}">
         {{view}}
     </option>
 </select>

Zamiast:

<select ng-model="config.list_view">
     <option ng-selected="{{view == config.list_view}}"
         ng-repeat="view in list_views"
         value="{{view}}">
         {{view}}
     </option>
 </select>

Sprawdziłem tablicę i nie mam żadnych duplikatów, po prostu sprawdź ponownie swoje zmienne.

 0
Author: Miguel Suarez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-18 16:11:03

Duplikaty w repeaterze są niedozwolone. Użyj wyrażenia "śledź według", aby określić unikalne klucze.

Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}

Przykład

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="angular.js"></script>

</head>
<body>
    <div ng-app="myApp" ng-controller="personController">
        <table>
            <tr> <th>First Name</th> <th>Last Name</th> </tr>
            <tr ng-repeat="person in people track by $index">
                <td>{{person.firstName}}</td>
                <td>{{person.lastName}}</td>
                <td><input type="button" value="Select" ng-click="showDetails($index)" /></td>
            </tr>

        </table> <hr />
        <table>
            <tr ng-repeat="person1 in items track by $index">
                <td>{{person1.firstName}}</td>
                <td>{{person1.lastName}}</td>
            </tr>
        </table>
        <span>   {{sayHello()}}</span>
    </div>
    <script> var myApp = angular.module("myApp", []);
        myApp.controller("personController", ['$scope', function ($scope)
        { 
            $scope.people = [{ firstName: "F1", lastName: "L1" },
                { firstName: "F2", lastName: "L2" }, 
                { firstName: "F3", lastName: "L3" }, 
                { firstName: "F4", lastName: "L4" }, 
                { firstName: "F5", lastName: "L5" }]
            $scope.items = [];
            $scope.selectedPerson = $scope.people[0];
            $scope.showDetails = function (ind) 
            { 
                $scope.selectedPerson = $scope.people[ind];
                $scope.items.push($scope.selectedPerson);
            }
            $scope.sayHello = function ()
            {
                return $scope.items.firstName;
            }
        }]) </script>
</body>
</html>
 0
Author: Yogesh Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-27 09:56:05

Jeśli wywołasz ng-repeat w tagu , możesz zezwolić na duplikaty. Zobacz ten link w celach informacyjnych. Patrz Todo2.html

 0
Author: TheUnKnown,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Moja odpowiedź JSON była taka:

{"items": 
  &nbsp;[
    &nbsp;&nbsp;{
      "index": 1,
      "name": "Samantha",
      "rarity": "Scarborough",
      "email": "[email protected]"
    },
    &nbsp;&nbsp;{
      "index": 2,
      "name": "Amanda",
      "rarity": "Vick",
      "email": "[email protected]"
    }]
}

Więc użyłem ng-repeat = "item in variables.items", aby go wyświetlić.

 -1
Author: Jay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-10 06:18:26

Duplikaty w repeaterze są niedozwolone. Użyj wyrażenia "śledź według", aby określić unikalne klucze. Repeater: sdetail in mydt, Duplicate key: string:, Duplicate value:

Napotkałem ten błąd, ponieważ napisałem źle Nazwa bazy danych w mojej części php api......

Tak więc ten błąd może wystąpić również podczas pobierania danych z bazy danych, której nazwa jest przez Ciebie zapisana nieprawidłowo.

 -1
Author: Vishal Solanki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-22 12:10:42