The Person Here
The Person Here

Reputation: 520

Why does ng-repeat return only one object from my MongoDB results?

In trying to answer this question, I decided to break down the problem into something simpler.

Problem: If I use ng-repeat to create rows for every element in "test", I instead get one row with one large JSON object.

HTML:

<div ng-view class="ng-scope">
    <table>
        <thead>
            <th>JSON for each object</th>
        </thead>
        <tbody>
            <tr ng-repeat="X in test">
                <td>{{ X }}</td>
            </tr>
        </tbody>
    </table>
</div>

Resulting JSON:

this is a HTML table where there should be more than one row with each object but there is only 1

test.js (clientApp.controller:TestCtrl)

'use strict';
angular.module('clientApp')
  .controller('TestCtrl', function (Test) {
    this.test = Test.getList().$object;
});

app.js

'use strict';

angular
  .module('clientApp', [
    'ngRoute',
    'restangular'
  ])
  .config(function ($routeProvider, $locationProvider, RestangularProvider
  ) {

    RestangularProvider.setBaseUrl('http://localhost:3000');

    $routeProvider
      .when('/test', {
        templateUrl: 'views/test.html',
        controller: 'TestCtrl',
        controllerAs: 'test'
      })

      $locationProvider.hashPrefix('');
  })
  .factory('TestRestangular', function(Restangular) {
    return Restangular.withConfig(function(RestangularConfigurer) {
        RestangularConfigurer.setRestangularFields({
            id: '_id'
        });
    });
  })
  .factory('Test', function(TestRestangular) {
    return TestRestangular.service('test');
  });

In summary: There should be more rows, one for every JSON object my test database, but instead I get one large JSON blob.

Update @therobinkim suggested that I change the ng-repeat query to "X in learn.learn", which provides the following interesting Restangular JSON stuff: enter image description here

Upvotes: 0

Views: 174

Answers (2)

therobinkim
therobinkim

Reputation: 2568

<tr ng-repeat="X in test.test">...</tr>

The first test is from controllerAs in your $routerProvider code.

The second test is from this.test in your TestCtrl code.

UPDATE: The $object in this.test = Test.getList().$object; is important to use in this format. Alternatively, you can use Promises. https://github.com/mgonto/restangular#enhanced-promises

Upvotes: 1

ronchi
ronchi

Reputation: 41

where is your test service? try to remove $object

'use strict';
   angular.module('clientApp')
     .controller('TestCtrl', function (Test) {
        //this.learn = Test.getList().$object;
    this.learn = Test.getList();
 });

Upvotes: 0

Related Questions