AngularJs #09 How To Use ng-show And ng-hide 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-show and ng-hide directive to hide and mask values.

  • How to add a new ng-model and apply the value of this model to the other directives
  • How to hide elements by the ng-hide and ng-show directive ( value of propriety of ng-model)
  • How to mask values by them ( ng-hide and ng-show )

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-hide and ng-show directive , why we use them ?
We use the ng-hide and ng-show directive to control the visibility of the HTML elements . I will explain it with an example .
I will create a table with 4 columns , by this directive when I check one of these columns will disappear and when I check again it will appear , let's see how to do it .
capture-20180217-145929.png
I have created an array named " employees " in a controller function this array contains 4 employees 4 objects each one has 4 elements " the name , gender , city and salary " , and I attached this array with the $scope to be the model in the view page .
capture-20180217-150434.png
In the HTML page I used the table to display the data of employees with a thead section to display the headers of our table and in the tbody section I used the ng-repeat directive to repeat each employee in employees and this is the result
capture-20180217-152035.png
This is how the employees data display at the moment just about this table we want t check box , so let's add it
capture-20180217-153724.png
I added an input with a type is check box and I have added an ng-model directive attribute with the value is hideSalary to link it with the columns and the $scope object also I added the label is " Hide Salary " also and this is the result
capture-20180217-153938.png
As you see I have the check box now but it's not clickable when you click nothing will change in the table to do it dynamically let's add the model here ..
capture-20180217-155146.png
I used the ng-hide directive here with the same propriety of the ng-model to be dynamically for the thead and the tbody data . Initially hideSalary not available because with the controller the $scope object we have only the employees but our variable hideSalary it's not available with the $scope object so it will be undefined so the value is false , what I did is the ng-hide directive it has the value false or true and now the value of the hideSalary is false so the ng-hide = false it means the column will display so when the page load we will be able to see the salary column and this is the result
capture-20180217-160025.png
Now when I check the hide salary what is going to happen the value of the ng-model hideSalary variable will be changed by this element and that will be updated in the model and the th , td elements that used this variable it will be true and when the ng-hide take the value true this is what will happen
capture-20180217-160841.png
As we check the checkbox the salary column is hidden and when we uncheck it will display again
capture-20180217-161020.png
Now we will use the ng-show directive we will change it just by type the " ! " sign which the opposite of our value so if the hideSalary variable has the value false it will be true because the ng-show = true it means the column will display but the opposite it's false it will be hidden
capture-20180217-161419.png
As you see here I take the opposite value if I have ng-hide directive and I use the opposite so the value it will be true and the column will be hidden by default but the ng-show it must be true to be displayed and this is the result
capture-20180217-161642.png
The column by default is true now I want to check it , it will be hidden because the value of the model will change and also the th and td elements will change also
capture-20180217-161708.png
After that let's mask and unmask the Salary column values it means when I check for hide salary the values will be masked then if I uncheck it , it will display
capture-20180217-162305.png
I have added another th and another td elements to divide one take the ng-hide directive and the other take the ng-show directive but the two elements has the same ng-model hideSalary propriety , if it's true they will be take the value and if it's false also they will be take it but there is a different because if it's true it will be show why ? because the ng-hide when it takes the true value it will be hidden so the salary will be hidden we will mask it but the opposite it will show and this is the result
capture-20180217-162408.png
Now the values are in the default presentation when we click on hide salary when we check it this is the result
capture-20180217-162429.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Education is the most powerful weapon which you can use to change the world. The capacity to learn is a gift, the ability to learn is a skill and the willingness to learn is a choice. The roots of education are bitter but their fruits is sweet, intelligence plus character that is the goal of true education...

stay blessed and thanks for sharing really love it, its also my subject..

Thank you for your reading and for this great comment , am happy to see you here good luck

pleasure is always mine!!!

and also thanks for you kind words...

Good info :D, i often use filters in my work

Great the filter it's very important and it's easy to create as I mentioned , thank's for your reading

A programming language is for thinking about programs,not for expressing programs you've already though of it should be a pencil not a pen.
stay blessed thanks for sharing the post:):)

Thank you for your reading and your comment , good luck

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