如何在ngRepeat中跳过$$ hashKey

时间:2018-06-01 15:02:14

标签: angularjs angularjs-ng-repeat

我试图从一个对象动态制作一些表。这是我的对象

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

app.controller("MainCtrl", function($scope) {
  $scope.data = {
    categories: ["kids", "home"],
    home: [{
      name: "home 1.1",
      title: " home1.2"
    }, {
      name: "home 2.1",
      title: "home 2.2"
    }, {
      name: "home 3.1",
      title: "home 3.2"
    }, {
      name: "home 4.1",
      title: "home 4.2"
    }, {
      name: "home 5.1",
      title: "home 5.2"
    }],
    kids: [{
      name: "kids 1.1",
      title: "kids 1.2"
    }]
  };


  $scope.getKeys = function(obj) {
    if (!$scope.data.hasOwnProperty(obj)) {
      return [];
    }

    return Object.keys($scope.data[obj][0]);
  }
});

我正在运行的代码是



<html ng-app="app">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
</head>

<body ng-controller="MainCtrl">
  <table ng-repeat="d in data.categories">
    <tr>
      <th ng-repeat="l in getKeys(d)" ng-if="$index != getKeys(d).length - 1">
        {{$index != getKeys(d).length - 1}} {{l}}
      </th>
    </tr>
    <tr ng-repeat="k in data[d]">
      <td>{{k.name}}</td>
      <td>{{k.title}}</td>
    </tr>
  </table>
</body>

</html>
&#13;
ng-if="$index != getKeys(d).length - 1"
&#13;
&#13;
&#13;

正如您在示例中所看到的,条件l != '$$hashKey'为false,但它显示 $$ hashKey 列。我怎么能跳过这个?

我尝试了一些this回答的解决方案:

  • angular.copy()
  • angular.toJson()
  • selectedPlayers { 0: {SHOT__Goals: 1.222222} 1: {SHOT__Goals: 0.888888} } const goals = ( selectedPlayers ) => { const goal= _.round((selectedPlayers.SHOT__Goals), 2).toFixed(2); return goals; }

现在:

  1. 为什么这种情况是错误的,但它显示了这个列?
  2. 获取每个表的列名的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您使用的是非常旧版本的AngularJS。这个问题很久以前就从版本1.2.0解决了。在下面的示例中我已经创建了它,它适用于从1.2.0到最近1.6.x的所有库版本

Plunker Example