Curriculum
Course: AngularJS
Login

Curriculum

AngularJS

AngularJS Tutorial

0/65
Text lesson

AngularJS Includes

In AngularJS, the ng-include directive allows you to embed HTML content.

Example

<body ng-app=””>

<div ng-include=”‘myFile.htm'”></div>

</body>

Include AngularJS Code

HTML files included using the ng-include directive can also contain AngularJS code.

myTable.htm:

<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.

Example

<body>

<div ng-app=”myApp” ng-controller=”customersCtrl”>
  <div ng-include=”‘myTable.htm'”></div>
</div>

<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>

Include Cross Domains

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.

Example:

<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.