{"id":2837,"date":"2019-07-02T10:30:36","date_gmt":"2019-07-02T15:30:36","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2837"},"modified":"2019-07-03T08:20:28","modified_gmt":"2019-07-03T13:20:28","slug":"angularjs-how-to-use-ng-show-to-hide-or-reveal-elements-on-click","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2019\/07\/02\/angularjs-how-to-use-ng-show-to-hide-or-reveal-elements-on-click\/","title":{"rendered":"AngularJS: How to use ng-show to hide or reveal elements on click"},"content":{"rendered":"\n<p>In this article, I will describe how to use the AngularJS directive <code>ng-show<\/code> for clicking a button to reveal hidden data. The steps below demonstrate how to use AngularJS to hide or reveal data based on clicking an element. In a combination of HTML and JavaScript, it uses AngularJS directives <code>ng-app<\/code>, <code>ng-controller<\/code>, <code>ng-click<\/code>, <code>ng-show<\/code>, and <code>ng-hide<\/code>. <\/p>\n\n\n\n<p>The app I will be walking through can be seen on codepen:<\/p>\n\n\n<p class='codepen'  data-height='400' data-theme-id='0' data-slug-hash='PrRQgG' data-default-tab='html,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen AngularJS: How to use ng-show to hide or reveal elements on click by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: AngularJS CDN<\/strong><\/h4>\n\n\n\n<p>To create an AngularJS app, the first thing we will need is an angular.js CDN in the script portion of our HTML document.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.7.8\/angular.min.js\">&lt;\/script><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: <\/strong>Set ng-app in HTML<\/h4>\n\n\n\n<p>In the main div container that will house the AngularJS scope, we need to define our app name using the <code>ng-app<\/code> directive. <br>For example: <code>ng-app=\"myApp\"<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div class=\"container\" ng-app=\"myApp\" ng-controller=\"controllerClickToHide\"><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: <\/strong>Mirror the app name in the JavaScript<\/h4>\n\n\n\n<p>The app name &#8220;myApp&#8221; that was defined in the HTML div container needs to be mirrored in the JavaScript using the <code>angular.module()<\/code> syntax shown below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var app = angular.module('myApp', [])<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: <\/strong>Set ng-controller in HTML<\/h4>\n\n\n\n<p>In the same div container where we defined the app name, we also need to set the name of the AngularJS controller for this app using the <code>ng-controller<\/code> directive. For example: <br><code>ng-controller=\"controllerClickToHide\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><br><strong>Step 5: <\/strong>Define this controller in JavaScript<\/h4>\n\n\n\n<p>In the JavaScript portion, we need to use this controller name as the first argument to the AngularJS <code>.controller()<\/code> function. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.controller('controllerClickToHide', function($scope) {<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 6: <\/strong>Set up the div in HTML with ng-show<\/h4>\n\n\n\n<p>In this step, we will set up the div that contains the hidden data. This is where <code>ng-show<\/code> should go. <code>ng-show<\/code> takes in an expression that evaluates to <code>true<\/code> or <code>false<\/code>. If the expression evaluates to true, then the element will be displayed. This element has the class name that is defined in the div with <code>ng-show<\/code>. In  this case, it is the class &#8220;hidden-data&#8221; that will be revealed. If the <code>ng-show<\/code> evaluates to false, then AngularJS applies an <code>ng-hide<\/code> class to the element. Here is what the div with the hidden data and <code>ng-show<\/code> looks like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div class=\"hidden-data\" ng-show=\"reveal\">\n   This div element will show \/ hide as you click the button!\n&lt;\/div><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 7: <\/strong>Set up the ng-click to toggle hidden data<\/h4>\n\n\n\n<p>The <code>ng-show<\/code> directive is set up to evaluate a variable called &#8220;reveal&#8221;. The <code>ng-click<\/code> directive will toggle &#8220;reveal&#8221; from true to false or from false to true. For example: <code>ng-click=\"reveal = !reveal\"<\/code><br>How this toggle works is, whatever the current value of reveal is (either true or false) <code>reveal = !reveal<\/code> will flip it to the opposite value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h4>\n\n\n\n<p>When the button is clicked to toggle reveal to false, <code>ng-hide<\/code> directive is applied to the class hidden-data, which sets the CSS display to none with an <code>!important<\/code> flag to make the element hidden. For example <code>display: none !important;<\/code>. When reveal is toggled to true, the <code>ng-hide<\/code> class is removed, making the hidden data visible. <br><br>Note: In order for this functionality to work, both the button div and the hidden content should be contained in the same container div. <\/p>\n\n\n\n<p>Here is the complete code:<\/p>\n\n\n\n<p><strong>HTML:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.7.8\/angular.min.js\">&lt;\/script>\n\n&lt;div class=\"container\" ng-app=\"myApp\" ng-controller=\"controllerClickToHide\">\n    &lt;h1>Demo of ng-show&lt;\/h1>\n\n      &lt;!-- This is the button div -->\n      &lt;div ng-click=\"reveal = !reveal\" class=\"buttonStyle\">\n        Show \/ Hide Information\n      &lt;\/div>\n  \nThis code demonstrates how to use AngularJS to hide or reveal data based on clicking an element. In a combination of HTML and JavaScript, it uses AngularJS directives ng-app, ng-controller, ng-click, ng-show, and ng-hide.\n      \n      &lt;!-- This is the hidden data div -->\n      &lt;div class=\"hidden-data\" ng-show=\"reveal\">\n        This div element will show \/ hide as you click the button!\n      &lt;\/div>\n    \n&lt;\/div><\/pre>\n\n\n\n<p><strong>CSS:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.hidden-data {\n  background: #3B505E;\n  height: auto;\n  width: 250px;\n  border-radius: 12px;\n  padding: 10px;\n  font-size: 15px;\n  color: #ffffff;  \n  margin-top: 15px;\n}\n\n.container {\n  width: 500px;\n}\n\n.buttonStyle {\n  background-color: #1565C0;\n  color: white;\n  border-radius: 8px;\n  font-size: 15px;\n  padding: 8px;\n  width: 180px;\n  text-align: center;\n  margin-bottom: 15px;\n  cursor: pointer;\n}<\/pre>\n\n\n\n<p><strong>JavaScript:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var app = angular.module('myApp', [])\n.controller('controllerClickToHide', function($scope) {\n  $scope.reveal = false;\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><br><strong>Links to more information<\/strong><\/h4>\n\n\n\n<p>ng-app<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngApp\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngApp<\/a><br>angular.module()<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/function\/angular.module\">https:\/\/docs.angularjs.org\/api\/ng\/function\/angular.module<\/a><br>ng-controller<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngController\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngController<\/a><br>ng-click<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClick\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngClick<\/a><br>ng-show<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngShow\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngShow<\/a><br>ng-hide<br><a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngHide\">https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngHide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will describe how to use the AngularJS directive ng-show for clicking a button to reveal hidden data. The steps below demonstrate how to use AngularJS to hide or reveal data based on clicking an element. In a combination of HTML and JavaScript, it uses AngularJS directives ng-app, ng-controller, ng-click, ng-show, and &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2019\/07\/02\/angularjs-how-to-use-ng-show-to-hide-or-reveal-elements-on-click\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">AngularJS: How to use ng-show to hide or reveal elements on click<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[148,44],"tags":[156,149,155,157,154,203,202],"class_list":["post-2837","post","type-post","status-publish","format-standard","hentry","category-angular","category-javascript-language","tag-angular-module","tag-angularjs","tag-ng-app","tag-ng-click","tag-ng-controller","tag-ng-hide","tag-ng-show"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2837"}],"version-history":[{"count":22,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2837\/revisions"}],"predecessor-version":[{"id":2888,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2837\/revisions\/2888"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}