How to get checkbox value in angularjs?

后端 未结 4 507
故里飘歌
故里飘歌 2021-01-04 09:00

I have a 10(or n)checkbox in my ng-view. Now I would like to ask here that What is the most efficient or simple way to check if checkbox is checked and if checked get the v

相关标签:
4条回答
  • 2021-01-04 09:45

    You could simply use angular filter here, also your ng-model should be album.Selected instead of folder.Selected

    Html

    {{(albums | filter : { Selected: true }: true)}}
    

    Controller

    $scope.albumNameArray = $filter('filter')($scope.albums, { Selected: true }, true);
    

    Before using it in controller don't forget to add $filter in your controller

    0 讨论(0)
  • 2021-01-04 09:47

    You can loop over the array to find out which is selected and push into the name array.

    <ul>
      <li ng-repeat="album in albums">
        <input type="checkbox" ng-model="album.selected" value={{album.name}} />
      </li>
    </ul>
    
    $scope.save = function(){
      $scope.albumNameArray = [];
      angular.forEach($scope.albums, function(album){
        if (!!album.selected) $scope.albumNameArray.push(album.name);
      })
    }
    
    // alternatively you can use Array.prototype.filter on newer browsers (IE 9+)
    $scope.save = function(){
      $scope.albumNameArray = $scope.albums.filter(function(album){
        return album.selected;
      });
    }
    

    jsbin

    0 讨论(0)
  • 2021-01-04 09:53

    If you want to get the value printed.
    You can use like :

    input type="checkbox" ng-model="checkboxModel.value2" ng-true-value="'YES'" ng-false-value="'NO'" 
    
    0 讨论(0)
  • 2021-01-04 10:01

    Have a look at this fiddle,i have used the object whose key is the album name

    <input type="checkbox" ng-model="folder[album.name]" value={{album.name}}/>

    controller:

    function ctrl($scope){
    $scope.albums = [{name:'a1'},{name:'a2'},{name:'a3'}];
    $scope.folder = {};
    $scope.albumNameArray = [];
    $scope.getAllSelected = function(){          
        angular.forEach($scope.folder,function(key,value){
            if(key)
                $scope.albumNameArray.push(value)
        });
    }}
    
    0 讨论(0)
提交回复
热议问题