需要澄清angularJs的每种方法

时间:2017-04-18 19:11:22

标签: javascript angularjs html5

我是角度js的新手,我试图使用ng-repeat获得独特的值。我浏览了很多网站,其中一个是enter link description here

我只从stackoverflow获得。 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,

  }
  ];
});

但我有一个问题需要理解,我已经花了好几个小时但仍然无法理解。

  **angular.forEach(collection, function(item) {
      var key = item[keyname];**
In forEach method in the function we pass value , index and then the object itself that we are iterating over, In the above example we are only passing the value as "item" , then in the next line we are using as object/array. I am really confused, what is it? what are they trrying to do

0 个答案:

没有答案