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

I work with an AngularJs/Laravel application. I try to access items in each facture. Actually I can't print out the whole facture object, but I wonder how to access the items so that I can loop trough them and display each.

Here is my Php Controller that send Json data

public function show($id)
{
    $facture = Facture::where('id', '=', $id)->with('items')->get();
    return Response::json($facture);
}

Here is my simplified AngularJs Controller

$http.get('api/factures/' + $stateParams.factureId).success(function(data) {
  $scope.facture = data;
});

And actually

{{facture |json}}
prints out this:

[
  {
    "id": 10200,
    "client_id": 1,
    "lead_id": 1,
    "courtedescription": "Description test",
    "etat": "En attente",
    "created_at": "2014-12-30 10:01:46",
    "updated_at": "2014-12-30 10:01:46",
    "items": [
      {
        "id": 1,
        "facture_id": 10200,
        "description": "Item numéro 1",
        "prix": "15.00",
        "tps": "0.75",
        "tvq": "1.50",
        "grandtotal": "17.25",
        "created_at": "2014-12-30 10:01:46",
        "updated_at": "2014-12-30 10:01:46"
      },
      {
        "id": 2,
        "facture_id": 10200,
        "description": "Deuxième item quoi",
        "prix": "135.00",
        "tps": "6.75",
        "tvq": "13.47",
        "grandtotal": "155.22",
        "created_at": "2014-12-30 10:01:46",
        "updated_at": "2014-12-30 10:01:46"
      }
    ]
  }
]

Here is a simplified Plunkr to focus on the essential: http://plnkr.co/edit/fZmb4fAX0GJCDH2cpxwQ?p=preview

How could I access the items?

share|improve this question
    
try this and you will see your items. facture[0].items –  wickY26 Feb 20 at 14:35
    
Great, now I'm looking how to loop trough so that I show properties –  Lucien Dubois Feb 20 at 14:53

1 Answer 1

up vote 1 down vote accepted

You can use the fucture value as a normal array because its within the views scope.

For instance if you want the id of the first object in the array you would use the following

{{facture[0].id |json}}

If you would like to loop over each value check out https://docs.angularjs.org/api/ng/directive/ngRepeat

Hope this helps!

share|improve this answer
    
Seems great as I can now access items propreties with {{facture[0].items[0].id}}. How could I loop trough items now? –  Lucien Dubois Feb 20 at 14:44
1  
Using NgRepeat: <body ng-controller="MainCtrl"> <div ng-repeat="facture in factures"> <h1>facture</h1> <pre ng-repeat="item in facture.items">{{item | json}}<pre> </div> </body> –  Alex Jones Feb 20 at 14:53
    
Make sure you change the facture array to factures. Details on how this works can be found here: docs.angularjs.org/api/ng/directive/ngRepeat –  Alex Jones Feb 20 at 14:53
    
Thank you! This works fine. –  Lucien Dubois Feb 20 at 14:58

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.