AngularJS | ng-include Directive
AngularJS has a built-in directive to include the functionality from other AngularJS files by using the ng-include directive.The primary purpose of the “ng-include directive” is used to fetch, compile and include an external HTML file in the main AngularJS application.These are added as child nodes in the main application. The ng-include attribute’s value can also be an expression, that returns a filename. All HTML element supports this.
Syntax:
<element ng-include="filename" onload="expression" autoscroll="expression" > Content...</element>
Note: Here the onload and autoscroll parameter are optional , onload define an expression to evaluate when the included file is loaded and autoscroll define whether or not the included section should be able to scroll into a specific view.
- Example:1
- External HTML file: Save this file as child.html.
<!-- child.html --><p>Hello Geeks from include component.</hp><!--I am a partial, i don't require head and body tags.--> - Code:
<!DOCTYPE html><html><head><title>AngularJS | ng-include Directive</title><scriptsrc=</script></head><bodyng-app=""style="text-align: center"><h1style="color:green">GeeksforGeeks</h1><h3>ng-include Directive</h3><divng-include="'child.html'"></div></body></html> - Output:

- Example 2:
- External HTML file: Save this file as table.html.
<!-- table.html --><table><trng-repeat="Subject in tutorials"><td>{{ Subject.Name }}</td><td>{{ Subject.Description }}</td></tr></table> - Code:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metahttp-equiv="X-UA-Compatible"content="ie=edge"><title>AngularJS | ng-include Directive</title><scriptsrc=</script></head><bodyng-app="main-app"><center><h1style="color: green;">GeeksforGeeks</h1><h3>ng-include Directive</h3><divng-controller="AngularController"><divng-include="'table.html'"></div></div><script>var main_app = angular.module('main-app', []);main_app.controller('AngularController', function($scope) {$scope.tutorials =[{Name: "AngularJS", Description : "Front End Framework"},{Name: "NodeJS", Description : "Server side JavaScript"},{Name: "ExpressJS", Description : "Server side JS Framework"}];});</script></body></html> - Output:



