JavaScript - Retrieve JSON File Using AngularJS

In this tutorial we will create a Retrieve JSON File Using AngularJS. AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, you will need to download the AngularJS here's the link https://angularjs.org/. Note: This code will work only if run in a local server.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Retrieve JSON File Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" type="button" ng-click="getData()"><span class="glyphicon glyphicon-plus"></span> Get JSON data</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Age</th>
  25. </tr>
  26. </thead>
  27. <tbody style="background-color:#fff;">
  28. <tr ng-repeat="member in members | orderBy: 'lastname'">
  29. <td>{{member.firstname}}</td>
  30. <td>{{member.lastname}}</td>
  31. <td>{{member.age}}</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </div>
  36. <script src="js/angular.js"></script>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the Script

This code contains the main function of the application. This code will fetch the data from a JSON file. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.
  1. var app = angular.module('myModule', [])
  2. .controller('myController', function($scope, $http){
  3.  
  4.  
  5. $scope.getData = function(){
  6. $http.get('data.json').then(function(response){
  7. $scope.members = response.data;
  8. });
  9. }
  10. });
There you have it we successfully created a Retrieve JSON File Using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment