JavaScript - Simple Change Template Using AngularJS

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

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


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.