0

I have my js for angular to fetch the json service from http and am using the {{post.title}} on my html to get the data and post to my html.

The data is not showing up on html page - using code pen.

var app = angular.module("blogApp", []); 
app.controller("mainCtrl",      function($scope) {
$scope.posts = []; 
let postsUrl ="https://jsonplaceholder.typicode.com/posts"
    getPosts().then(posts =>{
        $scope.posts = posts.slice(3);
        $scope.$apply();
    });

    function getPosts(){
        return fetch(postsUrl).then(res=>res.json());
    }

});

7
  • You may want to add the relevant HTML as well. As it stands, I can only guess it's because your code sets $scope.posts (which is an array?), not $scope.post Commented Nov 7, 2018 at 17:25
  • Could we see the JSON for posts, and your view html? Commented Nov 7, 2018 at 17:37
  • my html starts with: <div ng-app="blogApp" ng-controller="mainCtrl"></div> and then I have tried to call the post data with <li *ngFor="let post for posts$"> {{post.title}}</li> Commented Nov 7, 2018 at 17:43
  • @mediaguru the json is the service jsonplaceholder.typicode.com/posts Commented Nov 7, 2018 at 17:45
  • As I am using codepen to code, I left off the header tags in my html comment above. Commented Nov 7, 2018 at 17:49

1 Answer 1

1

I have seen your shared codepen. So Ricky as you are new to angularJS, I would suggest you to read the documentation related to angular 1 from here: Angular JS - Documentation

Now coming to your requirement, you required to call an external API and use the data from the result. For that you have to learn about the $http in angularJS : $http documentation

Coming to the code, angular supports the dependency injection. The code you have shared is a mystery for me like what fetch(postsUrl) function is doing? Where is the declaration?

Cut and short, the implementation should be clear and readable. Here is my refactored one:

var app = angular.module("blogApp", []); //here you defined the ng-app module

//you are initializing a controller, you need to inject $http for calling the API 
app.controller("mainCtrl", function($scope, $http) {

        //Declaration of the posts object
        $scope.posts = [];

        //Onetime initialization of the API Endpoint URL
        let postsUrl ="https://jsonplaceholder.typicode.com/posts";

        //A method for getting the posts
        function getPosts(){

           //We are calling API endpoint via GET request and waiting for the result which is a promise 
           //todo: Read about the Promises
           //A promise return 2 things either a success or a failure callback, you need to handle both. 
           //The first one is success and the second one is a failure callback
           //So in general the structure is as $http.get(...).then(successCallback, failureCallback)  
            $http.get(postsUrl).then(function(response){

               //In promises you get data in the property data, for a test you can log response like console.log(response)
               var data = response.data;

               $scope.posts = data; //Storing the data in the posts variable

                //Note: you don't need to call the $scope.$apply() because your request is with in the angular digest process. 
                //All the request which are outside the angular scope required a $apply()
            }, function(err){
               //log the err response here or show the notification you want to do
            });
        }

        //The final step is to call that function and it is simple
        getPosts();         

});

Coming to the second part to show the data. You have to use the ng-repeat documentation, it is as ng-repeat="var item in collection track by $index". It's documentation is here ng-repeat

So you html should be in this structure:

<div  ng-repeat="var post in posts track by $index"> 
    {{post.userid}}
    {{post.id}}
    {{post.title}}
    {{post.body}}
</div> 

Now it is onto you to learn and implement.

Sign up to request clarification or add additional context in comments.

2 Comments

hey Hassaan - WOW - thanks so much for the detailed explanation and answer - I really appreciate it. And your explanation makes total sense now. Thank you!
@Richy, please accept it as an answer if your query is done thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.