AngularJs #10 How To Use ng-init Directives in Angular JS ( Tutorial )

in #utopian-io8 years ago (edited)


Image Source

What Will I Learn?

I will learn how to use the ng-init directive to evaluate an expression and to use it with the ng-repeat directive.

  • How to use the ng-init directive to evaluate an expression ( array of employees in this example )
  • How to use the index of elements to prov the places of elements in our list
  • How to use the ng-init directive with the ng-repeat directive .

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-init directive in angular JS , this directive allows you to evaluate an expression in the current scope , let's explain what I mean by this sentence with an example .
capture-20180218-212411.png
I created a simple HTML page with a div , this div has an attribute which is the ng-init directive am going to use this directive to evaluate an expression which is the array of employees each employee has the name , gender and city proprieties since we are using the ng-init directive on this div element this employees array will be available with this div , now we should display these elements I have created a table with three table headers " Name , Gender and City " and I used the ng-repeat directive to loop the employees that we initialized and this is the result :
capture-20180218-215015.png
So here we just used the ng-init to evaluate the employees array it's basically initialize the data needs but in the real world application you should use a controller instead of ng-init directive to initialize values on a scope so that's the job of the controller , so when we used the ng-init to provide the data it's the responsability of the ng-controller
capture-20180218-220548.png
Here I have create a controller function with the name of control and the function with the $scope parameter and I just move the array to a variable and attache it with the scope object , we go to the HTML page
capture-20180218-221007.png
Here I used the ng-controller directive in a place of the ng-init directive and this is the same result :
capture-20180218-215015.png
So the ng-init directive should be used for aliasing special proprieties of ng-repeat directive let's understand what I mean by this statement with an example :
capture-20180218-221825.png
I created an array named " countries " in this array I have two elements the name of the country and the cities that it has for example I just choose two cities by the name of the city , let's go to the HTML view
capture-20180218-222245.png
To change a little and to be more logical I used a list into list to display the countries and the cities by the ng-repeat firstly I used the country in countries but in the second loop I used the city in the country but this city in the cities of this country and this is the result
capture-20180218-222309.png
So I have now the countries and tier cities by unordered list I want now to display the index of each element of these countries and this is how to do it
capture-20180218-232304.png
I used the $index propriety to display the index of our element and I just choose the ' index = ' expression to be more clear and this is the result
capture-20180218-232603.png
Look at the index so the first element has the index 0 and inside this element the first city also has the index 0 the second city has the index 1 also for the second county is 2 , now I want to display the parent index of our element , to do it I must save the parent index of our element for that I will use the ng-init directive ..
capture-20180218-233725.png
To use the parent index we must use the ng-init directive to save the value of index of the parent and just we call it we call this variable to put the value of our parent and this is the result
capture-20180218-233849.png
So the index of USA is 0 and for India is 1 so the cities of India has this value for parent index and the USA also and that what means ng-init directive should be used for aliasing special proprieties of ng-repeat .

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

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.081
BTC 61240.63
ETH 1620.29
USDT 1.00
SBD 0.42