How do I bind to list of checkbox values with AngularJS?

前端 未结 29 2703
天涯浪人
天涯浪人 2020-11-22 05:20

I have a few checkboxes:





        
29条回答
  •  萌比男神i
    2020-11-22 06:20

    In the HTML (supposing that the checkboxes are in the first column of every row in a table).

    
        
        
        
        ...
    
    

    In the controllers.js file:

    // The data initialization part...
    $scope.fruits = [
        {
          name: ....,
          color:....
        },
        {
          name: ....,
          color:....
        }
         ...
        ];
    
    // The checked or not data is stored in the object array elements themselves
    $scope.fruits.forEach(function(item){
        item.checked = false;
    });
    
    // The array to store checked fruit items
    $scope.checkedItems = [];
    
    // Every click on any checkbox will trigger the filter to find checked items
    $scope.getChecked = function(item){
        $scope.checkedItems = $filter("filter")($scope.fruits,{checked:true});
    };
    

提交回复
热议问题