JavaScript - Retrieve JSON File Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 168 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Retrieve JSON File Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button class="btn btn-primary" type="button" ng-click="getData()"><span class="glyphicon glyphicon-plus"></span> Get JSON data</button>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Age</th>
  26. </tr>
  27. </thead>
  28. <tbody style="background-color:#fff;">
  29. <tr ng-repeat="member in members | orderBy: 'lastname'">
  30. <td>{{member.firstname}}</td>
  31. <td>{{member.lastname}}</td>
  32. <td>{{member.age}}</td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. <script src="js/angular.js"></script>
  38. <script src="js/script.js"></script>
  39. </body>
  40. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.