ngInclude Directive In AngularJS


In this article, we will see how to use ngInclude directive in AngularJS, with an example. Also, we will learn what ngInclude directive is and what is its purpose.

For more articles on AngularJS, kindly refer to:

  • ngInclude directive is used to embed an HTML Page into another HTML Page.
  • This is used when you want to reuse the specific Page/View in multiple views, in your application.

The Value for ngInclude directive can be the name of the page which we want to include or property on $Scope object that points to the reusable feature of a page.

We will see this with an example. We have a controller function. In that controller function, we have an employee array.

  1. var mypartone = angular.module("mymodule", []).controller("myController"function($scope) {  
  2.     var employees = [{  
  3.         name: "Akshay",  
  4.         gender: 1,  
  5.         dateOfBirth: new Date("July 21,1960"),  
  6.         Address: "Mumbai",  
  7.         Salary: "1234.34"  
  8.     }, {  
  9.         name: "Milind",  
  10.         gender: 2,  
  11.         dateOfBirth: new Date("July 20,1960"),  
  12.         Address: "Nashik",  
  13.         Salary: "12.34"  
  14.     }, {  
  15.         name: "Raghvan",  
  16.         gender: 1,  
  17.         dateOfBirth: new Date("July 19,1960"),  
  18.         Address: "Pune",  
  19.         Salary: "1235.34"  
  20.     }, {  
  21.         name: "Hari",  
  22.         gender: 1,  
  23.         dateOfBirth: new Date("July 18,1960"),  
  24.         Address: "Mumbai",  
  25.         Salary: "5589.34"  
  26.     }];  
  27.     $scope.employees = employees;  
  28. });  
Now, we will add a page and represent the data on this. 
Go to solution; Add new Item ->Web form; name it as Angular1.aspx.

Add new Item


Add new Item

So, in that Angular1.aspx, we are going to use a table element in which <th> section will be used to display various headers and <thead> section. We are going to bind it with a binding expression as shown in the below code:
  1. <table>  
  2.     <thead>  
  3.         <tr>  
  4.             <th>Name</th>  
  5.             <th>Gender</th>  
  6.             <th>DOB</th>  
  7.             <th>Address</th>  
  8.             <th>Salary</th>  
  9.         </tr>  
  10.     </thead>  
  11.     <tbody>  
  12.         <tr ng-repeat="employee in employees">  
  13.             <td>{{}}</td>  
  14.             <td>{{employee.gender}}</td>  
  15.             <td>{{employee.dateOfBirth}}</td>  
  16.             <td>{{employee.Address}}</td>  
  17.             <td>{{employee.Salary}}</td>  
  18.         </tr>  
  19.     </tbody>  
  20. </table>  
Now, we want to represent this data in our main file, i.e., Angular.aspx. So, switch back to that file and write a single line, as shown below:
  1. <div>  
  2.     <div ng-include="'Angular1.aspx'">   
  3. </div>  
So, inside that div, we used ngInclude directive and referenced the page that we had represented in our data.

Now, just reload the page.


As you can see from the above output, the data got represented here. In our main file, we referenced the Angular1 Page by ngInclude directive .

Here, the value that we have represented on the page, can also be a $scope property.

Now, I am going to attach an employeeTest to $scope object and reference that page. 

$scope.employeeTest = "Angular1.aspx";

In our View, just write in ngInclude directive as employeeTest:

<div ng-include="employeeTest">

Now, reload the page. We get the exact same output as before.


Now, we will see another example. In that, if a user selects a table, it will display table. If he selects lists, it will display a list. Let's see:

Again, add a page and name it as Angular2.


As we want to include the list, we will take a <ul> and <li>. Now, in Angular2.aspx page, the code is:
  1. <ul ng-repeat="employee in employees">  
  2.     <li> {{}}  
  3.         <ul>  
  4.             <li> {{employee.Address}} </li>  
  5.             <li> {{employee.Salary}} </li>  
  6.         </ul>  
  7.     </li>  
  8. </ul>  
Now, let's go back to our main page angular.aspx. In that page, we will give a select option with two choices, i.e., List and Table.

Select By,
  1. <select ng-model="employeeTest">  
  2. <option value="Angular1.aspx">Table</option>  
  3. <option value="Angular2.aspx">List</option>  
  4. </select>  
In select, we passed ng-Model directive and in that, we passed employeeTest which we had attached to the $scope object.

In option value, we referenced the respective pages.

So, our final main page Angular.aspx page is.
  1. body ng-app="mymodule">    
  2. <div ng-controller="myController">  
Select By
  1. <select ng-model="employeeTest">  
  2. <option value="Angular1.aspx">Table</option>  
  3. <option value="Angular2.aspx">List</option>  
  4. </select>  
  5. <div ng-include="employeeTest"> </div>  
  6. </div>  
  7. </body>  
Now, just reload the page and see what output we get.




Conclusion: So, this was all about ngInclude directive in AngularJS.