AngularJS Includes: Reusing HTML Like a Pro - FadiZahhar/AngularJs GitHub Wiki
The StartApp Task Manager app is growing fast. The client says:
“We want the header, footer, and side menu to look the same on every page—but don’t copy-paste the HTML everywhere!”
Lina (Designer): “It’s a pain to change the header in ten places every time the logo updates.”
Mike (Team Lead):
“AngularJS has ng-include
. It lets us reuse HTML snippets—change once, update everywhere!”
Let’s say you have a header in header.html
:
<!-- header.html -->
<div class="w3-container w3-blue">
<h2>StartApp Task Manager</h2>
</div>
Main Page HTML:
<div ng-app>
<div ng-include="'header.html'"></div>
<div class="w3-container">
<p>Welcome to the dashboard!</p>
</div>
</div>
- The header file will be loaded and displayed wherever you put
ng-include
.
Create a sidebar (sidebar.html
):
<!-- sidebar.html -->
<div class="w3-sidebar w3-light-grey w3-bar-block" style="width:150px">
<a href="#" class="w3-bar-item w3-button">Dashboard</a>
<a href="#" class="w3-bar-item w3-button">Tasks</a>
<a href="#" class="w3-bar-item w3-button">Profile</a>
</div>
And a footer (footer.html
):
<!-- footer.html -->
<footer class="w3-container w3-dark-grey">
<p>© 2024 StartApp Team</p>
</footer>
Include them in your main page:
<div ng-app>
<div ng-include="'header.html'"></div>
<div ng-include="'sidebar.html'"></div>
<div class="w3-container" style="margin-left:160px">
<p>Welcome to the dashboard!</p>
</div>
<div ng-include="'footer.html'"></div>
</div>
Sara (Junior Dev): “We can also switch which file to include using an AngularJS variable!”
<select ng-model="view">
<option value="'about.html'">About</option>
<option value="'contact.html'">Contact</option>
</select>
<div ng-include="view"></div>
- The included content updates as the dropdown changes.
-
Keep reusable UI in separate
.html
files (headers, footers, navs, modals, etc.). - Change content once—see updates everywhere.
- Great for modular design, even before using component-based frameworks.
- Lina: “I only update the header in one file and it’s everywhere—amazing!”
- Sara: “This makes our code DRY (Don’t Repeat Yourself) and easy to maintain.”
- Mike: “Clean, modular code means faster updates and fewer bugs.”
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AngularJS Includes Example</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body>
<div ng-app="incApp" ng-controller="incCtrl">
<div ng-include="'header.html'"></div>
<div class="w3-row">
<div class="w3-col m2">
<div ng-include="'sidebar.html'"></div>
</div>
<div class="w3-col m10 w3-container">
<h4>Dashboard</h4>
<select ng-model="view">
<option value="'about.html'">About</option>
<option value="'contact.html'">Contact</option>
</select>
<div ng-include="view"></div>
</div>
</div>
<div ng-include="'footer.html'"></div>
</div>
<script>
angular.module('incApp', []).controller('incCtrl', function($scope) {
$scope.view = "'about.html'";
});
</script>
</body>
</html>
Create
header.html
,sidebar.html
,footer.html
,about.html
, andcontact.html
in your project folder.
-
ng-include
lets you break your app into reusable, maintainable HTML chunks. - One change updates every page—no copy-paste headaches.
- Makes teamwork and updates easier, especially on bigger projects.
Mike: “We’re finally DRY, not drowning in duplicate code!”
Lina: “Our UI is consistent and easy to update—let’s ship it!”
Try these examples live in the [W3Schools AngularJS TryIt Editor](https://www.w3schools.com/angular/angular_tryit.asp?filename=tryangular_includes).