angularjsapicheckboxionic-v1

Ionic/AngularJS checkbox and ng-repeat from API


The idea was to send true checkbox values from mobile to API so I can place an order with selected condiments, however I cant to get a grasp on it. Condiments are returned from HTTP GET call to API.

<ion-checkbox ng-repeat="condiment in condiments" ng-model="condiment.checked"
                          ng-checked="checkItem(condiment.id)">
                {{condiment.name}}
                {{condiment.price}}
</ion-checkbox>

It calls the function:

$scope.checkItem = function (id) {
            return $scope.condiments[id-1].checked = true;
};

(minus 1 is because ID starts at 1, and array at 0) But it is not called when checked/unchecked, but rather it makes all my resources checked by default, and once I click to uncheck them, nothing changes.

Property 'checked' is not part of the original JSON API output for the condiment. It has ID, name and price.

Question:

Is there a less painful way to send checked ID's back to server?

Edit:

I tried to set the default variables before, but that does nothing:

for(var i=0; i<$scope.condiments; i++){
            $scope.condiments[i].checked = false;
}

Solution

  • Me also Faced this kind of issue, I solved this by using like this,

     <input type="checkbox" ng-model="condiment.checked" ng-true-value="1"
            ng-false-value="0" ng-checked="condiment.checked == 1">
    

    This will Change your ng-checked True directly in json.. so after that simply console your API "condiments" you will get your answer..

    and if you want set checked by default False .simply use , ng-init="condiment.checked=0". so, if you checked it will return True else You will get False .