AngularJs Remove duplicate elements in ng-repeat

前端 未结 5 1028
醉话见心
醉话见心 2020-11-30 01:43

I have one dictionary which is stored in field_detail

  • {{field.displayName}}
  • 5条回答
    •  伪装坚强ぢ
      2020-11-30 02:32

      I worked up a JSFiddle based on the answer provided by Ben Leash:

      http://jsfiddle.net/ThunderHemlock/bvsvzrr5/1/

      Thanks, Ben. Other answers required the use of AngularJS UI or other such additional frameworks.

      var app = angular.module('app', []);
      
      app.filter('unique', function() {
         return function(collection, keyname) {
            var output = [], 
                keys = [];
      
            angular.forEach(collection, function(item) {
                var key = item[keyname];
                if(keys.indexOf(key) === -1) {
                    keys.push(key);
                    output.push(item);
                }
            });
            return output;
         };
      });
      
      app.controller('MyCtrl', function ($scope) {
      
      $scope.items = [
            {
              id : 1,
              column : "col1",
              comment : "col1-label1",
              text1 : "col1-label1-text1",
              checked: false,
      
            },
            {
              id : 2,
              column : "col1",
              comment : "col1-label2",
              text1 : "col1-label2-text1",
              checked: false,
      
            },
            {
              id : 3,
              column : "col2",
              comment : "col2-label1",
              text1 : "col2-label1-text1",
              checked: false,
      
            },
            {
              id : 4,
              column : "col2",
              comment : "col2-label2",
              text1 : "col2-label2-text1",
              checked: false,
      
            },
            {
              id : 5,
              column : "col3",
              comment : "col3-label1",
              text1 : "col3-label1-text1",
              checked: false,
      
            },
            {
              id : 6,
              column : "col3",
              comment : "col3-label2",
              text1 : "col3-label2-text1",
              checked: false,
      
            },
            {
              id : 7,
              column : "col4",
              comment : "col4-label1",
              text1 : "col4-label1-text1",
              checked: false,
      
            },
            {
              id : 8,
              column : "col4",
              comment : "col4-label2",
              text1 : "col4-label2-text1",
              checked: false,
      
            }
            ];
          });
      
      
      
      
      • {{ item.column }}

    提交回复
    热议问题