1

I am trying to show and hide a value based on the radio button selection. I am doing this on ng-click of radio button. It is not working for me

Here is the HTML and Angular js code

**HTML Code:**
<div ng-app="ssbApp" ng-controller="ssbCtrl">
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="single" ng-click="showHideTest=true">Show
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="multi" ng-click="showHideTest=false">hide
    <div ng-snow="showHideTest" ng-model="showHideTest">Test hide and show</div>
</div>

**Angular JS Code:**
var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
   $scope.showHideTest = false;
});

5 Answers 5

5

The reason your code doesn't work is there is a typo - ng-snow instead of ng-show.

You can also do this by binding the radio buttons to a boolean model (using ng-model), and use this variable to directly control the visibility of the div (using ng-show). Then you don't need any ng-click

HTML:

<div ng-app="ssbApp" ng-controller="ssbCtrl">
    <input type="radio" name="showHideExample" ng-model="showHideTest" ng-value="true">Show
    <input type="radio" name="showHideExample" ng-model="showHideTest" ng-value="false">hide
    <div ng-show="showHideTest">Test hide and show</div>
</div>

JS:

var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
    $scope.showHideTest = false;
});

Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

This is part of the code that I am working on. Along with radio buttons I had some drop downs. On change of the dropdown I have to get the results and display it in showHideTest area. Now when user clicks on show/hide radio buttons I have to reset the results. So for that I have to use ng-click
Does it work OK when you correct ng-snow -> ng-show?
OMG!!! that's the silly mistake I made. Now its working. Thanks a lot for correcting it.
@SriAlluri - why don't you choose sheilak as the answer.
2

One Simple Answer without even using js.. https://codepen.io/SusanneLundblad/pen/iBhoJ

<div ng-app="">
 <div class="wrap">
  <h1>Hello there!</h1>
   <p>Push the radio buttons to change the content!</p>
   <form> 
    <label for="first">Show first content</label>
     <input id="first" type="radio" name="content" ng-model="content" value="first">
  <br />
   <label for="other">Show other content</label>
     <input id="other" type="radio" name="content" ng-model="content" value="other">
   </form>

  <div class="wrapper">
   <p ng-show="content == 'first'">This is the first content!</p>
   <h2 ng-show="content == 'other'">This is the other content!</h2>
  </div>
 </div>
</div>

CSS: (just for clarity i used)

html, body {
background-color: #ecf0f1;
margin: 20px auto;
display: block;
max-width: 600px;
height: 100%;
}

body {
padding: 20px;
position: relative;
} 

label,h1,p,h2 {
font-family: "Helvetica Neue", sans-serif;
font-weight: 300;
}

h1 {
margin-bottom: 1.5em;
color: #3498db;
}

h2 {
color: #2980b9;
margin-bottom: 9px;
margin-top: 0;
font-size: 20px;
background-color: white;  
text-align: center;
padding: 16px;
z-index: 15;
border-radius: 4px;

transition: all 2s ease-out;
}

.wrap {
width: 320px;
margin: 0 auto;
display: block;
}

label {
display: inline-block;
width: 180px;
line-height: 2
}

form {
margin-bottom: 2em;
}


.wrapper {
border: 1px dashed #95a5a6;
height: 56px;
margin-top: 16px;
border-radius: 4px;
position: relative;
font-size: 12px;
}

.wrapper p {
line-height: 31px;
text-align: center;
} 

Comments

1
<div ng-app="ssbApp" ng-controller="ssbCtrl">
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="single" ng-click="showHide(true)">Show
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="multi" ng-click="showHide(false)">hide
    <div ng-snow="showHideTest" ng-model="showHideTest">Test hide and show</div>
</div>

**Angular JS Code:**
var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
   $scope.showHideTest = false;

   $scope.showHide = function(param){
 $scope.showHideTest = param;
}
});

Comments

1

simple and fast solution, the ng-init="yourmodel='val1'" is to set the initial value

<input type="radio" name="name"  ng-click="yourmodel='val1'"
       ng-checked="yourmodel=='val1'" ng-model="yourmodel"
       ng-init="yourmodel='val1'" > val 1
<input type="radio" name="name"  ng-click="yourmodel='val2'"
       ng-checked="yourmodel=='val2'" ng-model="yourmodel" > val 2

<div  ng-if="yourmodel=='val1'">  Radio 1 clicked </div>
<div  ng-if="yourmodel=='val2'">  Radio 2 clicked </div>

Comments

0

HTML Code Code:

Try this example, it will work.

<div ng-app="ssbApp" ng-controller="ssbCtrl">
    <input type="radio" name="showHideExample" ng-model="showHideExample"   ng-value="true">Show
    <input type="radio" name="showHideExample" ng-model="showHideExample" ng-value="">hide
    <div ng-snow="(showHideExample ==true)?true:false">Test hide and show</div>
</div>

Angular JS Code:

var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
  // $scope.showHideTest = false;
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.