2

Hi I just started using AngularJs, I am having issue when using $http to get data and $scope.productInfos = data returns Error: [ngRepeat:dupes], below is my code:

$scope.processForm = function(formData) {

        $http({

          method  : 'POST',

          url: '/quote-tool/productinfoforproductids/', // using php to generate json i.e [{id:1},{id:2}]

          data: $('.js-checkedCompareForm').serialize(),

          headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)

         }).success(function(result) {

            $scope.productInfos = result;

         }).error(function(err) {

             return err; 

        });
    };

in the view:

<div class="js-productInfo" ng-repeat="productInfo in productInfos">
    {{ productInfo.id }}
</div>

I am not sure angular see it as a repeated dup? I have been trying using track by $index, but still not working instead, it shows excessive repeats in the view.

Yogeshree Koyani
  • 1,649
  • 10
  • 19
Mike
  • 173
  • 1
  • 1
  • 12
  • to help you troubleshoot this problem, we would need to see an example of what is in `$scope.productInfos`, and what the incorrect output looks like. – Claies Oct 08 '15 at 05:07
  • Hi @Mike, could you please provide us the data that is returned from your http call? – Abhilash Augustine Oct 08 '15 at 08:00
  • @Mike, if you can't give the exact result, just give us example data. And check whether is there any duplicate entries are in your result as specified in the error message. – Abhilash Augustine Oct 08 '15 at 08:02

2 Answers2

0

This type of error occurs when you have duplicate keys in an ngRepeat expression.May be using track by $index will solve your problem.Look at this link : https://docs.angularjs.org/error/ngRepeat/dupes

<div class="js-productInfo" ng-repeat="productInfo in productInfos track by $index">
    {{ productInfo.id }}
</div> 

If you still get an error then please provide fiddle/plunker.

Juned Lanja
  • 1,466
  • 10
  • 21
0

I managed to find the problem, its caused by the php generated json file which generate a bad json format that causes the error in the data response.

was: {id:1},{id:2}

 should be:  {
    "productInfos":[
      {id:1},
      {id:2}
    ]
  }
Mike
  • 173
  • 1
  • 1
  • 12