简体   繁体   中英

Adding Comment feature to a JSON data using AngularJS

I took an AngularJS test yesterday and I was provided two task.

  1. I need to display the data of a JSON file on the web page in HTML form.

  2. User must be able to post comments on each post.

This is where I got stuck since I am not sure if it is possible. Is it possible to add a comment feature to an external JSON data such as the one I used? I just want to know the possibilities and the limitation of the external JSON file. Thank you.

This is my js file

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);
        };
    }]);

This is my 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>

If You have jsonobj=somevalue //array

var temperature = { temperature: { home: 24, work: 20 }};

jsonObj.data.push(temperature);

I think this sorted out my issue:

https://developer.wordpress.com/docs/api/console/

It allows me to extract data from their database on how many comments a post have then get the JSON data and display it on the site. :)

Case solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM