如何在 ngRepeat 中跳过 $$hashKey
How to skip $$hashKey in ngRepeat
我正在尝试从对象动态创建一些 table。这是我的对象
{
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"
}]
}
我的运行密码是
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>
如您在示例中所见,条件 ng-if="$index != getKeys(d).length - 1"
为假,但它显示了 $$hashKey 列。我怎样才能跳过这个??
我尝试了一些 this 答案的解决方案:
- angular.copy()
- angular.toJson()
l != '$$hashKey'
现在:
- 为什么这个条件为假,却显示了列?
- 获取每个 table 的列名称的正确方法是什么?
您使用的是非常旧的 AngularJS 版本。这个问题很久以前就从 1.2.0
版本解决了 检查下面我创建的示例,它适用于从 1.2.0
到最新版本 1.6.x
的所有库版本
我正在尝试从对象动态创建一些 table。这是我的对象
{
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"
}]
}
我的运行密码是
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>
如您在示例中所见,条件 ng-if="$index != getKeys(d).length - 1"
为假,但它显示了 $$hashKey 列。我怎样才能跳过这个??
我尝试了一些 this 答案的解决方案:
- angular.copy()
- angular.toJson()
l != '$$hashKey'
现在:
- 为什么这个条件为假,却显示了列?
- 获取每个 table 的列名称的正确方法是什么?
您使用的是非常旧的 AngularJS 版本。这个问题很久以前就从 1.2.0
版本解决了 检查下面我创建的示例,它适用于从 1.2.0
到最新版本 1.6.x