AngularJs #11 How To Use ng-include Directive in Angular JS ( Tutorial )

in #utopian-io8 years ago (edited)


Image Source

What Will I Learn?

I will learn how to use the ng-include directive in angular JS by two methods.

  • How to use the ng-include directive by value of HTML page directly
  • How to use the ng-include directive by a propriety of $scope object
  • How to include two HTML pages by select box that modify the value of propriety to your choice

Requirements

  • Basic Knowledge of HTML and CSS language .
  • Basic Knowledge of JavaScript language .
  • Has an editor text support HTML and JavaScript .
  • Must has the angular files from the official site .

Difficulty

  • Basic

Tutorial Contents

In this tutorial we will discuss how to use the ng-include directive in angular JS , so the ng-include is used to embed an HTML page into another HTML page , this technique is extremely useful when you want to reuse a specific view in multiple pages in your application .

The value for ng-include directive :

The value can be the name of the HTML page that you want to reuse like this
capture-20180219-175602.png
The name of our html page is utopian so I included it with this directive to show the page in our current page
capture-20180219-175757.png
And this is the second method to include an HTML page is to specify the name of the page with the $scope object
So the page can be a propriety on the $scope object that point to the reusable HTML page , let's understand them with example
capture-20180219-180622.png
In my controller function I created an array named " employees " this array has 4 objects each one of them has 3 elements the " name , gender and salary " and I attached it with $scope object which be going a model to the view , so all what we need to do is to present this array in HTML view by a table data for that I will add a new HTML file for this project and am going to call it EmployeeTable.html
capture-20180219-181924.png
Now we want to reuse this employees table HTML page inside the index HTML page
capture-20180219-182351.png
If you look in my index HTML page I have all the elements important , head and body so in the employeeTable.html page I will just create the table and remove the body and head sections
capture-20180219-183020.png
I used the table element to display the employees data I included the thead and tbody sections , inside the thead I used the th elements to display the headers of the table which are ' name , gender and salary ' and in the tbody section I have tr element has the attribute ng-repeat directive that's true when each employee in employees and inside the table data I used the binding expression to access to the name , city and gender of our employee
capture-20180220-002227.png
I created a div inside the body after using the ng-app and ng-controller directives to link it with the angular js module that I created , inside this div I added our ng-include attribute with the value between ' ' is the name of our page .html in our example the page is EmployeeTable.html and this is the example when we reload the page
capture-20180220-002651.png
As you see here the data is presented by a table in another page , now the value of ng-include is a name of another HTML page , the value can be also a propriety on the $scope object so let's see that ..
capture-20180220-003226.png
I attached a new propriety on the $scope object , this propriety has a value name of our HTML page
capture-20180220-003135.png
And I used just the name of our propriety as a model in the ng-include directive and this is the same result
capture-20180220-002651.png
Now I will show you another example to understand exactly how to use the ng-include directive , I will use two forms of views the first is to display the elements as a table and the second as a list how to do it , So the first thing is to add a new HTML page named " EmployeeList.html" because the other page named " EmployeeTable.html" I will again remove the body and the head and this is the code
capture-20180220-005312.png
So I used unordered list to display the data and by the ng-repeat directive the model employee in employees to loop the employees and for the first element we have just the name of the employee in the second list we have the salary and gender , so the second step is to create a select box in HTML view
capture-20180220-010702.png
I created a select box with an attribute ng-model attached with the $scope object , it has two options the first is table the second is list , when you click on the table option the value will be the link of our HTML employeeTable.html page and if you click on the second the value will be listEmployee.html so this value will be the value of the ng-model and it will be also the value of the ng-include directive by this option it will load the page that you selected and this is the result
capture-20180220-011135.png
This is the result when I clicked on the table option it will load the employeetable.html page that has the table of employees
capture-20180220-011154.png
This is the result also when I clicked on list the page of EmployeeList.html will be on the div .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you sir

Next time, please include the code examples as text format. Either directly in the article or e. g. as a GitHub-Gist.

Hey @vladimir-simovic, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @aymenz I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.076
BTC 64289.99
ETH 1665.81
USDT 1.00
SBD 0.41