Tuesday, February 14, 2017

$http service in AngularJS

As you'll may know, in my previous blog, I wrote about sending a HTTP request using AJAX (Asynchronous Javascript and XML). After writing it, I thought may I should play around it some more. So, I started writing it using AngularJS. For you all who does not have a clue what AngularJS is, it is an opensource javascript developed and maintained by Google to develop Single Page Applications (SPAs). It is based on the MVC (Model View Controller) architecture. I have started writing on AngularJS from the very beginning in my other blog. So if you guys are interested, please do drop by.

So back to where I should be. I used $http which is a service in AngularJS for reading from remote servers. It makes a request to the server and returns a response.

First I created a controller named 'contentController' within the angular module I used to define my angular application. The simply called the get method from the $http service to send my request to https://api.github.com/users. 'then' denotes the callback function upon a successful request. There I stored the received array of elements from the JSON string (response.data) into an attribute within $scope.



Then I used this controller in a div and used ngRepeat directive upon another div to repeat the required elements from the array to get the desired output. 


 Output



<<Nandun Bandara>>

No comments:

Post a Comment