AngularJS Includes
AngularJS Tutorial
- AngularJS Tutorial
- AngularJS Introduction
- AngularJS Expressions
- AngularJS Directives
- AngularJS Models
- AngularJS Scope
- AngularJS Controllers
- AngularJS Filters
- AngularJS Services
- AngularJS Http
- AngularJS Select
- AngularJS Tables
- AngularJS SQL
- AngularJS HTML DOM
- AngularJS Events
- AngularJS Modules
- AngularJS Forms
- AngularJS Input Validation
- AngularJS API
- AngularJS Bootstrap
- AngularJS Includes
- AngularJS Animations
- AngularJS Dependency Injection
- AngularJS Routing
- AngularJS Applications
AngularJS Examples
AngularJS Reference Manual
- AngularJS Reference Manual
- AngularJS Bootstrap
- AngularJS Animations
- AngularJS Includes
AngularJS Includes
In AngularJS, you can include HTML files within HTML.
Including HTML Files in HTML
In HTML, there is currently no built-in support for including HTML files.
Server-Side Includes
Most server-side scripts support including files (SSI: Server Side Includes).
Using SSI, you can include HTML files within HTML and send them to the client browser.
PHP Example
<?php require("navigation.php"); ?>
Client-Side Includes
There are many ways to include HTML files in HTML through JavaScript.
Typically, we use HTTP requests (AJAX) to fetch data from the server, and the returned data can be written into HTML elements using innerHTML.
AngularJS Includes
With AngularJS, you can use the ng-include directive to include HTML content:
Example
<body ng-app="">
<div ng-include="'.htm'"></div>
</body>
The steps are as follows:
Code for the .htm file:
<h1></h1>
<p>This is an included HTML page, implemented using the ng-include directive!</p>
Including AngularJS Code
The ng-include directive can not only include HTML files, but also include AngularJS code:
Code for the sites.htm file:
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Url }}</td>
</tr>
</table>
The included file "sites.php" contains AngularJS code, and it will be executed normally:
Example
<div ng-app="myApp" ng-controller="sitesCtrl">
<div ng-include="'sites.htm'"></div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sitesCtrl', function($scope, $http) {
$http.get("sites.php").then(function (response) {
$scope.names = response.data.records;
});
});
</script>
Cross-Domain Includes
By default, the ng-include directive does not allow including files from other domains.
If you need to include files from other domains, you need to set up a domain access whitelist:
Code for the sites.htm file:
<body ng-app="myApp">
<div ng-include="''"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
''
]);
});
</script>
</body>
Additionally, you also need to configure the server to allow cross-domain access. For configuration methods, refer to: PHP Ajax Cross-Domain Best Solutions.
Code for the angular_include.php file:
<?php
// Allow all domains to access
header('Access-Control-Allow-Origin:*');
echo '<b style="color:red">I am cross-domain content</b>';
?>
YouTip