mardi 24 novembre 2015

how can we get the array id of selected checkbox in angularjs?

I am trying to get the array id of selected checkbox but unable to get the array. I need the array in format of ["302740", "42006", "331497"]

here it is my app.js

var app = angular.module('app', []);

function Ctrl($scope) {     

    $scope.categories = [ {"tid":"302740","name":"2 BE 3"},{"tid":"42006","name":"A Line"},{"tid":"331497","name":"Activa"} ];
    $scope.brandlist = [];

    $scope.get = function (val) {
        console.log(val);

        var idx = $scope.brandlist.indexOf(val);

        if (idx > -1) 
            $scope.brandlist.splice(idx, 1);                
        else 
            $scope.brandlist.push(val);

        console.log($scope.brandlist);        
        // should come like selected array ["302740", "42006", "331497"] 
    }

}

and here it is the html

<div ng-controller="Ctrl">
    <span ng-repeat="(key,val) in categories">
        <label class="checkbox" for="{{key.tid}}">
            <input type="checkbox" ng-change="get(val.tid)" ng-model="brandlist[val.tid]" ng-checked="brandlist.indexOf(val.tid) > -1" name="group" id="{{val.tid}}" />
            {{val.name}}         
        </label>
    </span>
</div>

plunker




Aucun commentaire:

Enregistrer un commentaire