angularjs - 如何在 ngRepeat 中跳过 $$hashKey
问题描述
我正在尝试从对象动态创建一些表。这是我的对象
{
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列。我怎么能跳过这个?
我尝试了这个答案的一些解决方案:
- angular.copy()
- angular.toJson()
l != '$$hashKey'
现在:
- 为什么这个条件是假的,但它显示了列?
- 获取每个表的列名的正确方法是什么?
解决方案
您正在使用非常旧版本的 AngularJS。这个问题很久以前就从1.2.0
我创建的以下示例中的版本检查中解决了,它适用于从1.2.0
最新的 1.6.x 到所有版本的库
推荐阅读
- database - 如何使用laravel动态连接将数据输入数据库
- c - 如何使用崩溃实用程序分析 kdump 文件帧中的局部变量?
- python-3.x - 当我想在 aws lambda 函数中使用 selenium 时,如何上传 chromedriver?
- ssl - 在没有 SSL 验证的情况下调用 fetchgit
- postgresql - 从选择查询中删除重复项
- docker - 在 docker 镜像中执行命令以启动 solr exporter
- svg - 在 SVG 路径动画中为 SVG 设置动画
- powershell - 使用 new-item 创建文件夹偶尔会在 Powershell 中引发错误
- python - PyQt5 QTreeView 在检查子节点时自动折叠
- objective-c - 为什么我无法关闭 Touch ID 提示?