ng-repeat with empty objects

前端 未结 1 1941
情书的邮戳
情书的邮戳 2021-01-24 10:28

I\'m getting a \'Duplicates in repeater\' error. I read somewhere that I can track by index, but as soon as I do that all my object title and description values become duplicat

相关标签:
1条回答
  • 2021-01-24 10:53

    It's because you're adding the same object instance (stepTemplate) to the array each time. Angular sees that the array has multiple entries, but all of them point to the same object instance.

    The solution here is to create copies of your template, and add the copies to the array, not the template itself. You can create deep copies using angular.copy().

    $scope.addRow = function() {
        // create a deep copy of the step template
        var newStep = angular.copy(stepTemplate);
    
        // make any updates to it if necessary
        newStep.foo = 'bar';
    
        // add the new step to the list of steps, not the template itself
        $scope.item.steps.push(newStep);
    };
    
    0 讨论(0)
提交回复
热议问题