Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ng-repeat' How to get the `unique` values

Using an array, I am trying to filter and show the unique information in the list. For that I use the angular inbuild filter method.

But I am getting error.

Here is my try (I am filtering by SubProjectName)

<ul>
    <li ng-repeat="project in projectNames | unique: 'SubProjectName' ">
        {project.SubProjectName}}
    </li>
</ul>

Live Demo

like image 515
3gwebtrain Avatar asked Sep 29 '15 11:09

3gwebtrain


1 Answers

I think you are looking for an answer like this

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

app.controller('MainCtrl', function($scope) {
  $scope.projectNames=projects
  $scope.Id = "1";
  $scope.SubProjectName="Retail Building";

})
.filter('unique', function() {
  return function(projects, subProjectName) {
    var newprojects =[];
    projects.forEach(function(project){
      if(project.SubProjectName === subProjectName)
        newprojects.push(project);
    });
    return newprojects;
  };
});

<li ng-repeat="project in projectNames | unique:SubProjectName">{{project.SubProjectName}}</li>

Demo

like image 96
Shamal Perera Avatar answered Sep 20 '22 17:09

Shamal Perera