0

I'm using a Moltin Cart which I want to loop through, so I can read it in Angular JS and then

I load the cart into a variable, which I'd like to <div ng-repeat-start="item in cart.contents"> and loop through the items in the cart.

The issue I'm having is really a pretty basic JavaScript issue, that if I console.log the cart.contents I get this:

Object{ 
   33bb9f991617377b9b8333a79ca9ce2c: Object { ... cart contents... },
   1ba14b804d0d81e49397b004e5f1c6d1: Object { ... cart contents... },
   33bb9f991617377b9b8333a79ca9ce2c: Object { ... cart contents... },
}

Is there a way that I can fix this either in:

  • Laravel
  • Angular JS
  • or Classic JavaScript
4
  • What do you want instead? An array of objects? Commented Jan 5, 2015 at 21:14
  • You can angular.forEach it to get cart contents. Commented Jan 5, 2015 at 21:17
  • disclaimer: I'm a massive angular noob. How do I ng-repeat with a angular.forEach? Commented Jan 5, 2015 at 21:42
  • @Djave You would have to loop over the object in your controller with angular.forEach and populate an array with the values you want on each iteration. Then use new array as the object to which you will apply the ng-repeat directive. Commented Jan 5, 2015 at 21:46

1 Answer 1

1

I think what you're trying to do is this:

angular.module('myapp', [])
.controller('MyController', function($scope) {
    $scope.objectOfObjects = {
        "ASDF": { name: 'First', price: 100 },
        "QWER": { name: 'Second', price: 200 },
        "FGHJ": { name: 'Third', price: 500 }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp">
    <div ng-controller="MyController">
        When iterating over an object of objects, there is no guarantee about the order:
        <div ng-repeat-start="item in objectOfObjects">{{item.name}}</div>
        <div ng-repeat-end>{{item.price}}<br/><br/></div>
    </div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.