Angularjs ng重复:每隔一个值错误地显示为空白。
我的json(为可读性而编辑)版本如下所示 它实际上是json格式的reddit首页:Angularjs ng重复:每隔一个值错误地显示为空白。,angularjs,angularjs-ng-repeat,elements,empty-list,Angularjs,Angularjs Ng Repeat,Elements,Empty List,我的json(为可读性而编辑)版本如下所示 它实际上是json格式的reddit首页: { "kind": "Listing", "data": { "modhash": "fb7jljbeer6a0dbec0b7b939c890078b5a92d221b3e2b31cec", "children": [ { "kind": "t3", "data": {
{
"kind": "Listing",
"data": {
"modhash": "fb7jljbeer6a0dbec0b7b939c890078b5a92d221b3e2b31cec",
"children": [
{
"kind": "t3",
"data": {
"subreddit": "gifs",
"id": "66wssv",
"author": "GuacamoleFanatic",
"name": "t3_66wssv",
"subreddit_name_prefixed": "r/gifs",
"domain": "gfycat.com",
"thumbnail": "https://b.thumbs.redditmedia.com/E4FB3khTGQ_D8KP-P7KEVXpB9MzY_XDWUhtPmGnNOuE.jpg",
"subreddit_id": "t5_2qt55",
"permalink": "/r/gifs/comments/66wssv/8_year_old_tillys_first_couple_of_seconds_wearing/",
"url": "https://gfycat.com/VacantRequiredAmethystinepython",
"title": "8 year old Tilly's first couple of seconds wearing a bionic hand.",
"created_utc": 1492877987,
"num_comments": 504,
}
},
{
"kind": "t3",
"data": {
"subreddit": "IAmA",
"id": "66wut7",
"author": "JoergS",
"name": "t3_66wut7",
"subreddit_name_prefixed": "r/IAmA",
"domain": "self.IAmA",
"thumbnail": "self",
"subreddit_id": "t5_2qzb6",
"permalink": "/r/IAmA/comments/66wut7/iama_jörg_sprave_the_bald_crazy_german_who_runs/",
"url": "https://www.reddit.com/r/IAmA/comments/66wut7/iama_jörg_sprave_the_bald_crazy_german_who_runs/",
"title": "IamA (Jörg Sprave, the bald crazy German who runs \"The Slingshot Channel\" on YouTube.) AMA!",
"created_utc": 1492878607,
"num_comments": 781,
}
},
控制器非常简单:
angular.module('redditdupe').controller('frontPage', ['$scope', '$http', '$routeParams',
function frontPage($scope, $http){
$http({
method: 'GET',
url: 'https://www.reddit.com/.json'
})
.
then(
function successCallback(response)
{
$scope.children=response.data.data.children;
}
,
function errorCallback(response) {
}
);
}]);
html也是如此:
<div class="cartouche" ng-repeat="child in children">
<div ng-repeat="data in child">
<img src="{{ data.thumbnail }}"><br>
<a href="{{ data.url }}">{{ data.title }}</a> ({{ data.domain }})<br>
{{ data.created_utc * 1000 | date:'yyyy-MM-dd HH:mm:ss Z' }} <a href="https://www.reddit.com/user/{{data.author}}">{{data.author}}</a> <a href="#/{{ data.subreddit_name_prefixed }}">{{ data.subreddit_name_prefixed }}</a><br>
<a href="#/com/{{ data.subreddit_name_prefixed }}/comments/{{ data.id }}/.json">{{ data.num_comments }}</a><br>
</div>
</div>
({data.domain}})
{{data.created_utc*1000}日期:'yyyy-MM-dd HH:MM:ss Z'}
然而,ng重复显示
- 一组空白结果,后跟
- 一组真实的结果
- 然后是另一个空白集
- 接着是另一个实数集
- 。。。等等
<--- blank line
() <--- probably the domain gfycat.com, except it's blank
null < --- dunno
<--- probably the comment count, but is blank line
8 year old Tilly's first couple of seconds wearing a bionic hand. (gfycat.com)
2017-04-22 09:19:47 -0700 GuacamoleFanatic r/gifs
1959
() <--- then the same shenanigans again
null
Seen at the March for Science (i.redd.it)
2017-04-22 09:45:18 -0700 Polymathyx r/pics
338
您正在迭代子对象的所有值(种类、数据)。为什么要这样做而不是仅仅显示数据?我的理解是,我需要这样做,因为json是对象中重复嵌套的对象结构。每个文章都是一个节点,存储在“所有文章”节点中。因此,我需要循环遍历“所有文章”,并在一个边框内显示“每个文章”。您希望遍历子数组的所有元素。如果此数组的每个元素是一个具有两个属性的对象:种类和数据。所以你的外部ng重复是必要的(child中的child),正如我刚才所说的,child是一个具有两个属性的对象,种类和数据。没有理由让你的内心感到不安——重复一遍。你只想显示child.data.url、child.data.thumbnail等等。你是男人中的上帝。我非常感激。你也许帮我找到了理想的工作。标记为已接受。再次感谢。