javascript - Count items in array by using ng-repeat AngularJS -
i have jsonfile this:
vm.names = [{ 'name': 'a' }, { 'name': 'a' }, { 'name': 'b' }, { 'name': 'c' }, { 'name': 'c' }]
i using angularjs filter repeat in html:
<ul ng-repeat ="name in vm.names | unique'name'> <li>{{name.name}}</li> </ul>
here issue
so want count item, want display this
how can that?
just use groupby
filter provided angular-filter in runnable fiddle demo:
view
<div ng-controller="myctrl"> <ul ng-repeat="item in names | groupby: 'name' "> <li>{{item[0].name}}: {{ item.length}}</li> </ul> </div>
angularjs application
var myapp = angular.module('myapp', ['angular.filter']); myapp.controller('myctrl', function($scope) { $scope.names = [{ 'name': 'a' }, { 'name': 'a' }, { 'name': 'b' }, { 'name': 'c' }, { 'name': 'c' }]; });
Comments
Post a Comment