Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to display a list of my donors - the html is:

<div class="panel">
<header>
 <h1> Donor Information </h1>
</header>

<div class="content">
 <div ng-app="Donor">
  <div ng-controller="DonorCtrl">

  <ul>
   <li ng-repeat="donor in donors">{{donors.name_last | json}}</li>
  </ul>

   </div>
  </div>
 </div>
</div>

My Donor_controller.js is this:

var app;
app = angular.module("Donor", ["ngResource"]);

app.factory("Donors", [
   "$resource", function($resource) {
    return $resource("/donors", {}, {
        update: {
            method: "PUT"
        }
    });
  }
]);

app.factory("Donor", [
"$resource", function($resource) {
    return $resource("/donors/:id", {
        id: "@id"
    }, {
        update: {
            method: "GET"
        }
    });
  }
]);

this.DonorCtrl = [
 "$scope", "Donor", "Donors", function($scope, Donor, Donors) {
    var donor;
    $scope.donor = Donor.query();
    $scope.donors = Donors;
    $scope.addDonor = function() {};
    donor = Donor.save($scope.newDonor)(function() {
        return $scope.donors.push(donor);
    });
    return $scope.newDonor = {};
  }
];

I am returning from my rails app via donors/1.json this:

{
  "donor": {
  "id": 1,
  "tools_id": null,
  "first_name": "Billy",
  "last_name": "Bullard"
  }
}

I get a list of dots when I view and it shows this on inspection (repeated):

<li ng-repeat="donor in donors" class="ng-scope ng-binding"></li>

How do I go from the json coming from Rails to the list of names in Angularjs?

share|improve this question

1 Answer

You should remove | json from your bind and you want the donor's lastname, not the donors':

<li ng-repeat="donor in donors">{{donor.name_last}}</li>

Update 1

Also, you should have $scope.donors = Donors.query(); in your controller, not $scope.donor = ....

share|improve this answer
1  
additional note...json filter is for pretty print of object or array, typically only used for development debugging –  charlietfl Mar 30 at 15:19
 
Removed json {{donor.last_name}} get: TypeError: Cannot read property '$id' of undefined donor the model for the repeat shows: [ undefined, { 0: d } , { 0: i } , { 0: v } , { 0: } , –  tbrooke Mar 30 at 18:42
 
What are you returning for /donors/? Also, I just noticed that your array of donors is being saved saved to $scope.donor, not donors. And why are you setting $scope.donors = Donors? Shouldn't it be $scope.donors = Donor.query(); only? –  CaioToOn Mar 30 at 19:27
 
now have '$scope.donors = Donors.query();' same thing it returns basically an empty list - Actually in looking at the returned list in batarang the list is the html in my form { donor: { 0: d $$hashKey: 006 } } this d is the start of div - weird –  tbrooke Mar 30 at 20:39
 
Pretty weird, but what you see in Network response? Are you gettin an array of donors? Is you server returning an application/json? –  CaioToOn Mar 30 at 21:00
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.