Hit enter after type your search item

Learn using ng-show in AngularJS with 4 demos

The ng-show directive

In AngularJS, the ng-show directive is used to show or hide the specified elements.

For example, upon clicking a checkbox, you may display or hide a subscription form from the web page.

Similarly, you may use it with a div, paragraph, span for showing or hiding upon clicking a button or with other form control.

 

Syntax of using ng-show directive

The ng-show directive can be used as follows:

<button

ng-show=”expression”>

</button>

Where the button can be replaced by any other element that you want to associate ng-show.

A few main points about ng-show directive:

  • An expression is provided to the ng-show attribute.
  • If the given expression is evaluated as true, the element will display.
  • If the expression is false, the element will hide.
  • As the expression is false, the .ng-hide class applies to the specified element. The class sets the display with none value using !important flag (this is a built-in class of AngularJS). This makes the element hidden.
  • When the expression is true, the ng-hide class is removed from the element.

A demo of using ng-show with a button

In this demo, a button is created with Bootstrap class. The ng-click directive executes as you click the button and it will pass the Boolean expression to the controller. The div will display if it was hidden and vice versa:

angularjs ng-show

See online demo and code

The code:


 

A little custom CSS is also used for the div element:


 

That’s it!

An example with checkbox

The following demo shows using a checkbox for demonstrating ng-show directive. As you click the checkbox, it will display the hidden div. If that div is visible, clicking it again will hide it. No controller is used in this example:

angularjs ng-show checkbox

See online demo and code

The code of example:


 

Get the complete code from the demo page.

A demo of using expression to show entered color paragraph

In this demo, the ng-show is used with a paragraph where the entered color’s paragraph will display. As you enter a color name from the red, green, blue and yellow options in the textbox the entered value is evaluated and the respective paragraph with its specified class will display.

angularjs ng-show paragraph

See online demo and code

The markup:


 

The following CSS is used for paragraphs:


 

Same example with HTML select

In this demo, rather than using a textbox field, I have used an HTML select dropdown. Select any color from the dropdown and the respective color paragraph will display. In the <select> tag, the ng-model directive is used. This is followed by creating four paragraphs that are hidden initially. Each <p> tag is assigned an expression by using ng-show directive with its respective color name. The matched one will show, have a look:

angularjs ng-show select

See online demo and code

The markup:


 

Get the complete code from the demo page.

This div height required for enabling the sticky sidebar