Angularjs根据单选按钮选择显示和隐藏结果

时间:2015-09-28 16:12:06

标签: angularjs angularjs-ng-click

我试图根据单选按钮选择显示和隐藏值。我在单击单选按钮时执行此操作。这对我不起作用

这是HTML和Angular js代码

Bird

5 个答案:

答案 0 :(得分:5)

您的代码无法正常工作的原因是错误 - ng-snow而不是ng-show

您也可以通过将单选按钮绑定到布尔模型(使用ng-model)来执行此操作,并使用此变量直接控制div的可见性(使用ng-show)。那你就不需要任何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

答案 1 :(得分: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;
}
});

答案 2 :(得分:1)

一个简单的答案,甚至没有使用j .. 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 :(仅为了清楚我使用过)

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;
} 

答案 3 :(得分:1)

简单快捷的解决方案,ng-init="yourmodel='val1'"是设置初始值

<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>

答案 4 :(得分:0)

HTML代码:

  

试试这个例子,它会起作用。

<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代码:

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