Skip to main content

JavaScript - Simple Get Array Element Using AngularJS


In this tutorial we will create a Simple Get Array Element Using AngularJS. This code will retrieve and display an array when the user click the button The code itself use a angular directives that retrieve the array element in script then display it as a readable table format. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a comprehensive JavaScript framework that extend the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request in a whole.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it 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="containet-fluid">
  10. <a class="navbar-brand" href = "https://www.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 - Simple Get Array Element Using AngularJS</h3>
  16. <hr style = "border-top:1px dotted #ccc;"/>
  17. <div class = "container-fluid">
  18. <button ng-click="getArray();" class="btn btn-primary">Get Array</button>
  19. <br /><br />
  20. <table class = "table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Student ID</th>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Grade</th>
  27. </tr>
  28. </thead>
  29. <tr ng-repeat = "student in students">
  30. <td>{{$index+1}}</td>
  31. <td>{{student.firstname}}</td>
  32. <td>{{student.lastname}}</td>
  33. <td>{{student.grade}}</td>
  34. </tr>
  35. </tbody>
  36. </table>
  37. </div>
  38. </div>
  39. </body>
  40. <script src ="js/jquery-3.2.1.min.js"></script>
  41. <script src = "js/angular.js"></script>
  42. <script src = "js/script.js"></script>
  43. <script src ="js/bootstrap.js"></script>
  44. </html>

Creating the Main Function

This code contains the main function of the application. This code will display an array when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3.  
  4. $scope.arrays = [
  5. {firstname: "John", lastname: "Smith", grade: "2"},
  6. {firstname: "Claire", lastname: "Temple", grade: "8"},
  7. {firstname: "Ash", lastname: "Ketchump", grade: "4"},
  8. ];
  9.  
  10. $scope.getArray = function(){
  11. $scope.students = $scope.arrays;
  12. }
  13. });

There you have it we successfully created a Simple Get Array Element using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.