Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table that I want to populate using ng-repeat. I'm having trouble figuring out how to format my array so that it works. Here is what I have so far:

PHP (customers.php):

$customer_array = array();

$customer1 = array(
  'customer_id' => '1',
  'name' => 'John Doe',
  'city' => 'New York'
);
$customer_array[] = $customer1;

$customer2 = array(
  'customer_id' => '2',
  'name' => 'Jane Doe',
  'city' => 'Boston'
);
$customer_array[] = $customer2;

echo($customer_array);

AngularJS Controller (CustomersCtrl.js):

$http({
    url: 'customers.php',
    method: "POST"
})
.success(function (data, status) {
    $scope.customers = data;
})
.error(function (data, status) {
    $scope.status = status;
});

HTML:

<table>
<tbody ng-controller="CustomersCtrl">
    <tr ng-repeat="customer in customers">
        <td>{{customer.customer_id}}</td>
        <td>{{customer.name}}</td>
        <td>{{customer.city}}</td>
    </tr>
</tbody>
</table>

This isn't working. What am I doing wrong?

UPDATE: I'm wondering if this is because I'm loading the HTML code from above using the code below:

$routeProvider.when('/customers.php', {
    templateUrl: 'partials/customers.html',
    controller: 'CustomersCtrl'
});

Is it because the newly loaded html template is not bound to the controller before it tries to do the ng-repeat loop?

share|improve this question
add comment

1 Answer

You need to JSON encode the array before sending your response.

echo json_encode($customer_array);

That will give your javascript code something to work with. Right now the only thing getting passed back to your javascript is the word 'Array' which won't do anything.

share|improve this answer
 
I added json_encode but it still doesn't work. I've used this feature a lot with ul li, so I'm not sure why this isn't working. –  Stephanie Caldwell Jul 6 '13 at 19:18
 
I'm starting to wonder if angular is worth it. This seems like such a simple thing to do, yet I've spent all day on it with no luck! Beyond frustrating. –  Stephanie Caldwell Jul 6 '13 at 21:45
 
What does the JavaScript console in your browser say? What do you get if you do a console.log($scope.customers) inside your success function? What does the network tab in the Google Chrome inspector (or similar tool) say is being returned from the server? Is your success function even being called? We can attempt to help you with specific problems, but "still doesn't work" isn't enough to go on when we can't see the actual error on your screen. –  Brandon Tilley Jul 9 '13 at 3:12
add comment

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.