In AngularJS, the ng-include
directive allows you to embed HTML content.
<body ng-app=””> <div ng-include=”‘myFile.htm'”></div> </body> |
HTML files included using the ng-include directive can also contain AngularJS code.
<table> <tr ng-repeat=”x in names”> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table> |
By including the file “myTable.htm” in your web page, all AngularJS code will be executed, including the code within the included file.
<body>
<div ng-app=”myApp” ng-controller=”customersCtrl”> <script> var app = angular.module(‘myApp’, []);
app.controller(‘customersCtrl’, function($scope, $http) { $http.get(“customers.php”).then(function (response) { $scope.names = response.data.records; }); }); </script>
|
By default, the ng-include directive prevents including files from other domains. To allow files from external domains, you can configure a whitelist of permitted files and/or domains in your application’s config function.
<body ng-app=”myApp”>
<div ng-include=”‘https://tryit.w3schools.com/angular_include.php'”></div> <script> var app = angular.module(‘myApp’, [])
app.config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ ‘https://tryit.w3schools.com/**’ ]); }); </script>
</body> |
Ensure that the destination server permits cross-domain file access. |