0

Can someone advise why the following is not working.

Controller:

(function () {
'use strict';

angular
    .module("shop")
    .controller("CartSummaryController", CartSummaryController);

function CartSummaryController($scope, cart) {

    $scope.cartData = cart.getProducts();

    console.log($scope.cartData);

    $scope.total = function () {
        var total = 0;
        for (var i = 0; i < $scope.cartData.length; i++) {
            total += ($scope.cartData[i].price * $scope.cartData[i].count);
        }
        return total;
    }

    $scope.remove = function (id) {
        cart.removeProduct(id);
    }


}

})();

Html:

Your Cart

<div ng-show="cartData.length == 0">
    There are no products in your shopping cart.
</div>

<div ng-hide="cartData.length == 0">
    <table class="table">
        <thead>
            <tr>
                <th>Quantity</th>
                <th>Item</th>
                <th class="text-right">Price</th>
                <th class="text-right">Subtotal</th>
            </tr>
        </thead>
    </table>

    <tbody>
        <tr ng-repeat="item in cartData">
            <td>{{item.count}}</td>
            <td>{{item.name}}</td>
            <td>{{item.price | currency}}</td>
            <td>blah</td>
        </tr> 
    </tbody>

</div>

Object printed to console:

Object count : 1 id : "1" name : "item #1" price : "100.00"

Furthermore, is there a way to debug the ng-repeat?

5
  • Please use console.log(angular.toJson($scope.cartData));, and post the output. Commented Oct 1, 2016 at 15:46
  • [{"count":1,"id":"1","price":"100.00","name":"item #1"}] Commented Oct 1, 2016 at 15:46
  • Did you give your ng-app and ng-controller names correct? Can you show rest of HTML page as well? Commented Oct 1, 2016 at 15:47
  • What happens when you add {{ cartData }} to the template? If it doesn't display anything, the problem is in code you don't show. Try posting a complete example in a plunkr. Commented Oct 1, 2016 at 15:49
  • @JBNizet when i print {{ cartData }} the object prints to the screen. Commented Oct 1, 2016 at 15:54

1 Answer 1

3

The label </table> should go after the label </tbody>

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

2 Comments

Excellent, I knew it would be something as simple as that. Thanks dude!
@ClarkySmiverz77 You're welcome and I'm glad it has been solved.

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.