0

This is most likely a simple solution, but I have been unsuccessful in adding a counter to show how many items are in a list using ng-repeat. When i use {{lists.count}} it does add a counter but it does it in multiples of 10. What is the correct way to count the items in this simple grocery list app?

<div class="container" ng-controller="shopCtrl">
<div class="col-lg-12 well">
    <h1>Grocery List</h1>
    <form class="form-inline" role="form">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Enter Something" ng-model="addToList">
        </div>

        <button type="submit" class="btn btn-default" ng-click="addItem()">Submit</button>
    </form>
    <div class="col-lg-3">
        <ul class="list-unstyled listItems">
            <li>
                <a href="#">
                    <span class="badge pull-right">{{list.count}}</span>
                    Total Items
                </a>
            </li>
        </ul>
        <ul class="list-unstyled g-list">

            <li id="listed" ng-repeat="list in lists">{{list}} <button ng-click="deleteItem(list)" class="btn btn-danger sm">x</button></li>
        </ul>
    </div>
</div>

function shopCtrl($scope){
$scope.addToList;

$scope.lists = [];

$scope.addItem = function(){
    $scope.lists.push($scope.addToList);

    $scope.addToList = '';
};

$scope.deleteItem = function(deletedItem){
    var idx = $scope.lists.indexOf(deletedItem);
    $scope.lists.splice(idx,1);
};

}

2
  • use lists.length instead of list.count Commented Mar 4, 2014 at 11:54
  • read angular doc and use trackBy $index Commented Mar 4, 2014 at 12:43

2 Answers 2

2

You can get the number of items of an array with the length property. Just use:

{{lists.length}}
Sign up to request clarification or add additional context in comments.

Comments

0

just replace

<span class="badge pull-right">{{list.count}}</span>

with

<span class="badge pull-right">{{lists.length}}</span>

1 Comment

Geeezz..I knew it was going to be something as simple as that. Thank you so much for your answer!!

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.