Shimu
Shimu

Reputation: 1147

ng-repeat repeats too often

Recently, I started to take a look into angular.js. I have managed to integrate it within a rails app now, but angular isn't working as it should.
This means, it repeats an object too often. Lets say, I have an Array with 2 JSONs and I want to repeat every single json.comment. When I have 2 jsons, I get every single comment twice, if I have 3 of them, I get every single comment three times. Here is the code:

var logbookApp = angular.module("logbookApp",[]);

logbookApp.controller("LogbookCtrl", function($scope){
    $scope.entries = [
        {comment: "One"},
        {comment: "Two"},
        {comment: "Three"}
    ];
});

And here is the markup:

  <html ng-app="logbookApp">
  <body ng-controller="LogbookCtrl">
   <div class="view-container">
    <div ng-view class="view-frame"></div>

    <ul>
       <li ng-repeat="entry in entries">
           {{entry.comment}}
       </li>
    </ul>
    <%= yield %>
  </div>
 </body>
</html>

Where is my mistake?

Upvotes: 1

Views: 2498

Answers (1)

Andyrooger
Andyrooger

Reputation: 6746

As this appears to have fixed the problem, I'll add as a full answer for the benefit of those stumbling across this question in future:

ng-repeat can create excess repeated items when the angular library is included more than once. From my brief testing I would expect there to be n^a items shown for n items expected in the repeat, and a versions of angular included in the page.

I would guess this is due to the first angular inclusion processing the ng-repeat directive on the initial element correctly. Subsequent inclusions then re-process the directive which is still present on each repeated item.

Upvotes: 3

Related Questions