2015-11-25 51 views
0

我想在選中一個或多個複選框時顯示帶有選項的div,但我似乎無法弄清楚ng-show=""代碼應該是什麼。什麼是這個jQuery代碼的AngularJS等價物?AngularJS相當於jQuery:檢查代碼

$('.checkboxes:checked').length > 0

回答

1

,我發現我的解決方案:

對於複選框,中號使用:

<div ng-repeat="item in items"> 

<input type="checkbox" ng-model='item.selected' /> 

</div> 

而且ng-show=""ng-show="(items|filter:{selected:true}).length"

謝謝您的回答!

0

不,優雅,但也許你可以使用這個:

<!doctype html> 
<html ng-app="app"> 
    <head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script> 
    <script> 
     var app = angular.module('app', []); 
     app.controller('MyCtrl', function ($scope) { 
      $scope.options = [ 
       {'title' : 'Title1', 'value' : 'A'}, 
       {'title' : 'Title2', 'value' : 'B'}, 
       {'title' : 'Title3', 'value' : 'C'} 
      ]; 

      $scope.checkboxes = {}; 

      $scope.showMessage = function() {     
       for (var key in $scope.checkboxes) { 
       if ($scope.checkboxes[key] === true) return true; 
       }     
       return false; 
      } 
     });       
    </script> 
    </head> 
    <body> 
    <div ng-controller="MyCtrl">   
     <label ng-repeat="option in options"> 
     <input 
      type="checkbox" 
      value="{{option.value}}" 
      ng-model="checkboxes[option.value]" 
     > {{option.title}} 
     </label>  

     Options: {{checkboxes|json}}<br> 
     Show message: {{showMessage()|json}}<br> 
     <div ng-show="showMessage()"> 
     hey 
     </div>   
    </div> 
    </body> 
</html> 

http://plnkr.co/edit/nVCmukG5abpi1Y4ZHkrq?p=preview