2015-10-14 81 views
0

我昨天参加了一个AngularJS测试,并且提供了两项任务。使用AngularJS向JSON数据添加注释功能

  1. 我需要在HTML表单中的网页上显示JSON文件的数据。

  2. 用户必须能够在每篇文章发表评论。

这是我被卡住的地方,因为我不确定它是否可能。是否可以将注释功能添加到外部JSON数据,例如我使用的数据?我只想知道外部JSON文件的可能性和限制。谢谢。

这是我的JS文件

var myapp = angular.module('myapp', ['ui.bootstrap']); 

myapp.controller('mainCtrl', function($scope, $http) { 

    $http.get("https://public-api.wordpress.com/rest/v1/freshly-pressed") 

    .success(function(response) { 
     $scope.names = response.posts; 
    }); 
}); 

angular.module('myapp') 
    .filter('to_trusted', ['$sce', function($sce){ 
     return function(text) { 
      return $sce.trustAsHtml(text); 
     }; 
    }]); 

这是我的HTML

<body data-ng-controller="mainCtrl" data-ng-app="myapp"> 

<div data-ng-repeat="p in names" class="container"> 
<img data-ng-src="{{ p.author.avatar_URL }}"><br/> 
Author: {{ p.author.nice_name }}<br/> 
URL: <a href="{{ p.author.URL }}">{{ p.author.URL }}</a><br/> 
Title: {{ p.title }}<br/> 
Content:<br/> 
<div data-ng-bind-html="p.content | to_trusted"></div><br/> 
Comments: {{ p.comments_open }} 
</div> 
</body> 
+3

只需将'comments:[]'属性添加到要迭代的对象并推送注释即可。 – tymeJV

+1

我不认为你会攻击WordPress.com并改变他们的JSON提要。对您的应用程序本地化的评论系统可能没问题;-) –

+0

Alvaro G. Vicario - 是的,我认为,我相信JSON文件中提供的内容是您可以在页面上显示的唯一数据。正确? – Neil

回答

0

如果您有jsonobj = someValue中//阵列

VAR温度= {温度:{家:24 ,work:20}};

jsonObj.data.push(temperature);