Skip to main content

JavaScript - Simple Change Template Using AngularJS


In this tutorial we will create a Simple Change Template Using AngularJS. This code will change the current template when the user select a value the in the dropdown box. The code itself use a angular directives that can fetch an external html code in order to change the current template in the web page. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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"s>
  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://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 Change Template Using AngularJS</h3>
  16. <hr style - "border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <div class = "form-inline">
  20. <label class = "text-info">Change Template:</label>
  21. <select class = "form-control" ng-model = "viewData">
  22. <option value = "employee_table.html">Table</option>
  23. <option value = "employee_list.html">List</option>
  24. </select>
  25. </div>
  26. <br />
  27. <div ng-include="viewData"></div>
  28. </div>
  29. </div>
  30. <script src="js/angular.js"></script>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Main Function

This code contains the main function of the application. This code will change the current template when the use select an option in the dropbox. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.

script.js
Note: Make sure you save this file inside the js directory to make the application work.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. var employees = [
  4. {name: "Tony Stark", age: "42", address: "New York"},
  5. {name: "Steve Rogers", age: "36", address: "Nazi"},
  6. {name: "Natasha Romanof", age: "28", address: "Berlin"},
  7. {name: "Bruce Banner", age: "35", address: "London"},
  8. {name: "Thor Odinson", age: "89", address: "Asgard"}
  9. ];
  10. $scope.employees = employees;
  11. $scope.viewData = "employee_list.html";
  12. });

employee_table.html

  1. <table class="table table-bordered">
  2. <thead class="alert-info">
  3. <tr>
  4. <th>Name</th>
  5. <th>Age</th>
  6. <th>Address</th>
  7. </tr>
  8. </thead>
  9. <tr ng-repeat="employee in employees">
  10. <td>{{employee.name}}</td>
  11. <td>{{employee.age}}</td>
  12. <td>{{employee.address}}</td>
  13. </tr>
  14. </tbody>

employee_list.html

  1. <div ng-repeat="employee in employees" >
  2. <ul style="list-style-type:none; padding:5px 5px 5px 5px;">
  3. <li>Name: {{employee.name}}</li>
  4. <li>Age: {{employee.age}}</li>
  5. <li>Address: {{employee.address}}</li>
  6. </ul>
  7. </div>

There you have it we successfully created a Simple Change Template 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.