Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show a particular alphabet item of array in ng-repeat?

i have two arrays one for alphabets and second for tags.. So i want to show only same alphabets items in that particular alphabet category example -- apple has to come in A category and zoo has to come in Z category...

fiddle link http://jsfiddle.net/4dGxn/149/

html

<div class="form-group" ng-controller="mainCtrl">
  <div ng-repeat="alp in alpha">
  <h2>{{alp}}</h2>
    <ul>
      <li ng-repeat="tag in tags">{{tag}}</li>
    </ul>
  </div>
</div>

angular

var app = angular.module("app", []);

app.controller("mainCtrl", ['$scope', function($scope){

  $scope.alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

  $scope.tags = ["apple", "dog", "cat", "dad", "baby", "zoo", "love", "hate", "rat", "room", "home", "age", "bad"];

}]);
like image 830
Akshay Kumar Avatar asked Dec 07 '22 23:12

Akshay Kumar


1 Answers

You can use str.startsWith to check if element starts with a particular character So in your example:

<div class="form-group" ng-controller="mainCtrl">
  <div ng-repeat="alp in alpha">
  <h2>{{alp}}</h2>
    <ul>
      <li ng-repeat="tag in tags" ng-show="{{tag.startsWith(alp)}}">{{tag}}</li>
    </ul>
  </div>
</div>

Here is the wirking Fiddle

like image 191
Naeem Shaikh Avatar answered Dec 11 '22 10:12

Naeem Shaikh