22

How to find array size in AngularJS

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Looping with objects:</p>
<ul>
  <li ng-repeat="x in names">
    {{ x.name}}
  </li>
</ul>

</div>
    $scope.names = [
        {name:'Jani'} ,
        {name:'raaj'} 
    ];
});
</script>

</body>
</html>

here i need size of names array

3
  • You mean, like $scope.names.length ? Commented Mar 7, 2016 at 17:51
  • 1
    You did not specify if you wanted to display the length in the view, or use it in the controller, so, where do you want to use it? Commented Mar 7, 2016 at 17:53
  • in view i want to display count Commented Mar 7, 2016 at 18:03

2 Answers 2

55

Just use the length property of a JavaScript array like so:

$scope.names.length

Also, I don't see a starting <script> tag in your code.

If you want the length inside your view, do it like so:

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

1 Comment

It works but in console also print 'Cannot read property 'length' of undefined'
6

You can find the number of members in a Javascript array by using its length property:

var number = $scope.names.length;

Docs - Array.prototype.length

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.