0

here's the demo:

JSFiddle

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

    function Ctrl($scope) {

        $scope.selection = [];

        $scope.categories = [ { "name": "Sport", "id": "50d5ad" } ,
        {"name": "General", "id": "678ffr" } ];

    }

If i check any checkbox I need to see it's name in the array, but nothing happens, why ?

3 Answers 3

2

I'd suggest adding a function that either add or delete an element on click, depending on the state of your checkbox :

Updated JSFIDDLE

HTML :

<input type="checkbox" ng-click="addToSelection(category)" ng-model="category.selected" name="group" id="{{category.id}}"/>

Controller :

$scope.addToSelection = function(category) {
            if (category.selected == true)
          $scope.selection.push(category.name);
        else
        $scope.selection.splice($scope.selection.indexOf(category.name),1);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You have to bind listener for ng-change event. Something like

ng-change="selection.push(category.name)"

Updated fiddle: http://jsfiddle.net/0m3qc4s6/2/

Comments

0

You could try to change

$scope.selection = [];

with

$scope.selection = {};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.