AngularJS | Includes

Embedding html page within html is not supported by HTML that’s why we used AngularJS Include Directives. By using ng-controller directive we can easily do the task.

Syntax:

<element ng-include=" ">content...<element>

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <script src=
    </script>
    <title>ng-include directives</title>
</head>
   
<body ng-app="">
    <center>
        <h1 style="color:green;">GeeksforGeeks</h1>
        <div ng-include="'geeks.html'"></div>
    </center>
</body>
</html>

chevron_right


Output:

Including AngularJS code:Similar to previous case you include the html file by using ng-include similarly it can contain AngularJS code.

Example:

GeeksforGeeks.html table:

filter_none

edit
close

play_arrow

link
brightness_4
code

<table>
  <tr ng-repeat="x in courses">
    <td>{{ x.Course }}</td>
    <td>{{ x.Duration }}</td>
  </tr>
</table>

chevron_right


Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
  <script src=
  </script>
</head>
<body>
  <div ng-app="geeks" ng-controller="customersCtrl"
    <div ng-include="'Geekstable.html'"></div>
  </div>
  <script>
    var app = angular.module('geeks', []);
    app.controller('customersCtrl', function($scope, $http) {
         $http.get("customers.php").then(function (response) {
             $scope.courses = response.data.records;
             });
         });
  </script>
</body>
</html>

chevron_right


Output:

Include Cross Domains:If you want to include files from another domain then you can add a whitelist of legal files or domains in the config function of your application.
Sample code:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <script src=
  </script>
<body ng-app="myApp">
  <div ng-include="'filel_path_from_anotherDomain'"></div>
  <script>
    var app = angular.module('myApp', [])
    app.config(function($sceDelegateProvider) {
        $sceDelegateProvider.resourceUrlWhitelist(['filel_path_from_anotherDomain']);
    });
  </script>
</body>
</html>

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.