Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 value of checkbox.

<ul>
  <li ng-repeat="album in albums">
   <input type="checkbox" ng-model="folder.Selected" value={{album.name}} />
   </li>
  </ul>

I have ng-controller(getAlbumsCtrl), in that I have an array in which I want to push all the checkedbox albums names into albumNameArray

share|improve this question
up vote 11 down vote accepted

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);
  })
}

jsbin

share|improve this answer
    
I think if (!!album.selected) should be simple 'if (album.selected) { $scope.albumNameArray.push(album.name);' } But when I doing so.. I am getting (album.selected) = undefined. However, just for checking purpose I did value = {{album.selected}} it is true..but when in controller it is giving me undefined error.. – Kgn-web Jun 27 '15 at 19:19
    
This is what I am doing JavaScript:- angular.forEach($scope.albums, function (album) { if (album.selected) { $scope.albumNames.push(album.name); } }); HTML :- <input type="checkbox" class="checkbox-inline" ng-model="album.selected" value="{{album.name}}" /> I am getting album.selected = undefined. – Kgn-web Jun 27 '15 at 19:58
    
sorry I didn't see the checkbox value. Normally I don't put value when I use checkbox, just using ng-model. If value has an effect on the model, can you check whether the model is already the name itself instead of the object? – Icycool Jun 28 '15 at 4:29
1  
added jsbin for demo – Icycool Jun 29 '15 at 2:52
1  
I might not be knowledgeable enough on routes to answer this question, you can open a new one so others that know more about this may answer. – Icycool Jun 29 '15 at 7:03

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)
    });
}}
share|improve this answer

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

share|improve this answer
    
imp for me... $scope.save = function() { } $scope.read = function() {} suppose I want to call save function from read..because when I am doing I get save() not defined.. I am doing in this way $scope.read = function() { save(); } – Kgn-web Jun 27 '15 at 20:29
1  
@Chetan as both method in scope then it should be $scope.read = function() { $scope.save(); } – Pankaj Parkar Jun 27 '15 at 20:34
    
@Pankaj...yeah right... thank you – Kgn-web Jun 28 '15 at 5:43
    
I have 2 configurations in my routeConfig.. one is $routeProvider. when('/albums', { templateUrl: 'xyz.html', controller: 'getAlbumsCtrl' }) .when('/Album/:AlbumName', { templateUrl: 'xyz.html', controller: 'getAlbums12Ctrl' }) in getAlbums12Ctrl(), I am colllecting AlbumName is $routeParams, but what I need is whatever follows Album/* please collect it in AlbumName($routeParams) @Pankaj like this ** /albums/bollywood >>(AlbumName = "bollywood")** */albums/bollywood/old >> (AlbumName = "bollywood/old)" /albums/ – Kgn-web Jun 29 '15 at 6:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.