AngularJS includes
AngularJS includes
In AngularJS, you can include HTML files in HTML.
Including HTML files in HTML
Currently, including HTML files is not supported in HTML.
Server-side include
Most server-side scripts support file inclusion (SSIServer Side Includes).
Using SSI, you can include HTML files in HTML and send them to the client browser.
PHP instance
<
?php
require("navigation.php"); ?
Client-side inclusion
There are many ways to include HTML files in HTML using JavaScript.
Usually, we use http requests (AJAX) to get data from the server, and the returned data can be written into an HTML element usinginnerHTMLAngularJS includes
Using AngularJS, you can use
ng-includeng-includeInstructions to include HTML content:
Example
<
body
ng-app
=
"
"
<
div
ng-include
=
"
'runoob.htm'
"
</
div
</
bodyTry it »
The steps are as follows:
runoob.htm file code:
<
h1
Beginner Tutorial
</
h1
<
pThis is an included HTML page, implemented using the ng-include directive!
</
p
Contains AngularJS code
The ng-include directive can include not only HTML files but also AngularJS code:
sites.htm file code:
<
table
<
tr
ng-repeat
=
"x in names
"
<
td
{{ x.Name }}
</
td
<
td{{ x.Url }}
</
td
</
tr
</
tableThe file "sites.php" contains AngularJS code that will be executed normally:
instance
<
div
ng-app
=
"
myApp
"
ng-controller
=
"
sitesCtrl
"
<
div
ng-include
=
"
'sites.htm'
"
</
div
</
div
<
scriptvar app = angular.module('myApp', []); app.controller('sitesCtrl', function($scope, $http) { $http.get("sites.php").then(function (response) { $scope.names = response.data.records; }); });
</
script
Try it »
Cross-domain inclusion
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 a domain access whitelist:
sites.htm file code:
<
body
ng-app
=
"
myApp
"
<
div
ng-include
=
"
'https://c.runoob.com/runoobtest/angular_include.php'
"
</
div
<
scriptvar app = angular.module('myApp', []) app.config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ 'https://c.runoob.com/runoobtest/**' ]); });
</
script
</
bodyTry it »
In addition, you need to set up the server to allow cross-domain access. For setting methods, please refer to:PHP Ajax Cross-Domain Problem Best Solution.