AngularJS如果检查复选框显示

时间:2016-08-24 20:47:47

标签: javascript angularjs

有人能告诉我为ng-repeat创建If / Else语句的正确语法吗? 简而言之,如果选中框,则显示最大数字范围。如果未选中显示最小值。 我搜索过并搜索过,但是我找不到在页面上而不是在控制器内部执行此操作的语法。

**json**
{
    "min": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
    "max": ["91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
}

**Inside my controller declaring scope**
$scope.min = response.data.min;
$scope.max = response.data.max;

**display.html**
<input type="checkbox" name="min-max" id="min-max" class="css-min-max"     checked="checked"/>

<div ng-repeat="info in min"> <--- this is my problem. 
      <p>{{info}}<p>
</div>

4 个答案:

答案 0 :(得分:1)

也许您正在寻找的东西比您描述的用例更多(其他答案中都有很好的解决方案)

这里有很长的路要走:

<body ng-app="listByCheckbox" >
  <div ng-controller="listDisplayCtrl">
     <input type="checkbox" ng-model="checkedList"   ng-init="checkedList=true"/> 
       Check the box for low numbers, un-check box for high numbers
       <div ng-value= "selectedList = checkedList ? listOptions.min : listOptions.max"  >
          <p ng-repeat="info in selectedList">{{info}} is a fine number.<p>     
       </div>
   </div>
</body>

使用没有逻辑的控制器,如下所示:

var app = angular.module('listByCheckbox', []) 

  app.controller('listDisplayCtrl', function ($scope) {

    $scope.listOptions = {
      "min": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
      "max": ["91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
    }

  })

fiddle

答案 1 :(得分:0)

你可以使用ng-show(复选框为ng-mode)并有两个html节点或者有一个过滤器。

&#13;
&#13;
<label class="item item-text-wrap" ng-show="checked"></label>
<label class="item item-text-wrap" ng-show="notchecked"></label>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用控制器的方法调用动态选择集合。

控制器:

$scope.selected = false;
$scope.choose = function() {
    return $scope.selected ? $scope.max : $scope.min;
}

HTML:

<input ... ng-model="selected">
<div ng-repeat="info in choose()">

答案 3 :(得分:0)

将您的HTML修改为

<input id="some_id" type="checkbox" ng-checked="minmax">

<div ng-repeat="info in repeatData">
<p>{{info}}</p>
</div>

将以下内容添加到您的控制器

if(minmax){
$scope.repeatData=response.data.max;
}
else
{
$scope.repeatData=response.data.min;
}