Analysis and Solutions for AngularJS ng-repeat Duplicates Error

Nov 21, 2025 · Programming · 14 views · 7.8

Keywords: AngularJS | ng-repeat | track by | duplicates error | custom filters

Abstract: This article provides an in-depth analysis of the 'Duplicates in a repeater are not allowed' error in AngularJS ng-repeat directive. Through practical case studies, it demonstrates issues with custom filters in nested ng-repeat structures, explains the principles and application scenarios of track by expressions, and offers comprehensive solutions and best practice recommendations.

Problem Background and Error Analysis

In AngularJS development, the ng-repeat directive is one of the core tools for building dynamic lists. However, when dealing with arrays containing duplicate data, developers often encounter the “Duplicates in a repeater are not allowed” error message. This error stems from AngularJS's design mechanism, which requires each item in ng-repeat to have a unique identifier.

From the provided case, the problem occurs in a nested ng-repeat structure:

<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>

The custom range filter is implemented as follows:

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;
    };
});

Root Cause Investigation

The fundamental cause of this error lies in AngularJS's ng-repeat directive needing to generate unique DOM elements for each repeated item. When duplicate values exist in the array, AngularJS cannot determine how to properly track and update the state of these elements. In the original case, the array returned by the range filter may contain duplicate numerical values, causing ng-repeat to fail.

A simple example clearly demonstrates this issue:

// 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]">

Solution: track by Expression

AngularJS provides the track by expression to resolve duplicate item issues. By adding a track by clause to ng-repeat, developers can explicitly specify the property or expression used to uniquely identify each item.

For the above problem, the simplest solution is to use $index:

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

In the original case, the nested ng-repeat should be modified to:

<div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2 track by $index">

Advanced Applications of track by

Beyond using $index, track by can also ensure uniqueness based on specific properties of objects. This is particularly important when dealing with arrays of objects:

// Assuming each object in the comments array has a unique id property
<div ng-repeat="comment in item.comments track by comment.id">

This approach not only solves the duplicate item problem but also improves performance, as AngularJS can track and update DOM elements more efficiently.

Related Case Analysis

The ui-select case from the reference article further illustrates the prevalence of this issue. In that case, even with track by $index, duplicate item errors still occurred. In-depth analysis revealed that the problem might stem from data binding mechanisms or special handling by third-party libraries.

This case reminds us that in certain complex scenarios, a deeper understanding of data flow and AngularJS's digest cycle may be necessary:

<ui-select-choices repeat='role in roles track by $index'>
    {{role}}
</ui-select-choices>

Best Practice Recommendations

1. Always add track by expressions to ng-repeat that may contain duplicate values

2. Prefer using unique object identifiers over $index for better performance

3. Ensure data uniqueness in custom filters

4. Carefully examine the tracking mechanism of each ng-repeat in complex nested structures

Performance Optimization Considerations

Using appropriate track by expressions not only resolves duplicate item errors but also significantly enhances application performance. When data changes, AngularJS can precisely determine which DOM elements need updating, adding, or deleting based on track by values, avoiding unnecessary re-rendering.

This optimization is particularly important in large datasets. For example, when processing lists containing thousands of records, proper track by strategies can substantially reduce the overhead of DOM operations.

Conclusion

The AngularJS ng-repeat duplicates error is a common but easily solvable problem. By understanding the mechanisms behind the error and correctly using track by expressions, developers can build more robust, high-performance web applications. Whether dealing with simple numerical arrays or complex object structures, appropriate tracking strategies are key to ensuring stable application operation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.