2

次のAngularJSコードを使用しています...

<div ng-controller="MyCtrl">

   <h4>I have {{friends.length}} friends. They are...</h4>

<span id="selectall" ng-click="selectAllFriends()"><u>Select All</u></span>  <br>

<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="friend in friends">
<input 
type="checkbox" 
value="{{friend.id}}"
ng-checked="friend.checked"
ng-model="friend.checked"
>
{{friend.id}} {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>

<pre><strong>{{selectedFriends().length}} selected with filter:</strong> {{friends | filter:{checked:true} | json}}</pre>

<script>
function MyCtrl($scope, $filter) {
  // fruits
  $scope.friends = [
                        {id: 1, name:'John', age:25, gender:'boy'},
                        {id: 2, name:'Jessie', age:30, gender:'girl'},
                        {id: 3, name:'Johanna', age:28, gender:'girl'},
                        {id: 4, name:'Joy', age:15, gender:'girl'},
                        {id: 5, name:'Mary', age:28, gender:'girl'},
                        {id: 6, name:'Peter', age:95, gender:'boy'},
                        {id: 7, name:'Sebastian', age:50, gender:'boy'},
                        {id: 8, name:'Erika', age:27, gender:'girl'},
                        {id: 9, name:'Patrick', age:40, gender:'boy'},
                        {id: 10, name:'Samantha', age:60, gender:'girl'}
                    ];

    $scope.selectAllFriends = function() {
        angular.forEach($scope.friends, function(friend){
            friend.checked = true;
        });
    };

  $scope.selectedFriends = function () {
    return $filter('filter')($scope.friends, {checked: true});
  };
}
</script>

ここにjsfiddleがあります

すべて選択機能は正常に機能していますが、追加したいのは、[すべて選択] をクリックした後、そのテキストを [すべて選択解除] に変更し、すべてのチェックボックスを選択解除する必要があることです。トグルを使用して可能だと思います。ありがとう。

4

3 に答える 3

3
$scope.selectAllFriends = function() {
    angular.forEach($scope.friends, function(friend){
        friend.checked = true;
    });
};

に置き換えます:

$scope.isAll = false;               
    $scope.selectAllFriends = function() {

        if($scope.isAll === false) {
        angular.forEach($scope.friends, function(friend){
            friend.checked = true;
        });
    $scope.isAll = true;        
    } else {
    angular.forEach($scope.friends, function(friend){
            friend.checked = false;
        });
    $scope.isAll = false;       
    }
    };

デモ

于 2013-09-17T09:53:12.783 に答える