0

I've seen a few posts asking the same question but I can't make it work. I'm quite new to angular so I would need help.
I'm trying to insert a new income with tags.
I get thoses tags from a service and display them like this :

<label ng-repeat="tag in tags">
    <input type="checkbox" ng-model="tags_chosen" name="tags_chosen[tag]" ng-true-value="<%tag.id%>"/> <%tag.name%>
</label>

When I try to get back the checkbox values in angular, it doesn't work :

 this.addIncome = function($scope) {
 var data = {
        'project_id':$scope.project_id,
        'amount':$scope.amount,
        'payment_date':$scope.payment_date,
        'tags':$scope.tag_chosen,
        'description':$scope.description,
        'type':$scope.type
    };

    return $http.post(URL.BASE_API + 'income/store',data).
    success(function(response) {
        ServicesStatus.return = response;
    }).error(function(response) {
        console.log('Service error');
    });
};

How could I do that ?

Thanks !

1 Answer 1

1

try this:

$scope.tag_chosen =[];

$scope.toggleSelection = function ( deviceId, $event ) {
   var checkbox = $event.target;
   var action=(checkbox.checked ? 'add':'remove');

    var idx = $scope.tag_chosen.indexOf( deviceId );

    // is currently selected
    if (action=='remove' && idx != -1 ) { 

        $scope.tag_chosen .splice( idx, 1 );

    }

    // is newly selected
    if (action=='add' && idx == -1 ) {

        $scope.tag_chosen.push( deviceId );
    }

and in html >>

ng-click="toggleSelection(yourcjeckbox value,$event)"
Sign up to request clarification or add additional context in comments.

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.