2015-10-14 32 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);