0

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

3 回答 3

1

试试这个,这是为你创建的小提琴。看到这个

<div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <div ng-repeat="(key,val) in condiments">
            <input type="checkbox" ng-model="selected[val.id]" 
                   ng-click="checked(key,selected[val.id])"
                   ng-true-value="true" ng-false-value="false">
              {{val.name}}
        </div>
        <br>
        <br>
        <div>added status into main array
            <br>{{condiments}}</div>
    </div>
</div>

这是控制器和逻辑

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

    function MyCtrl($scope) {
        $scope.selected = {};
        $scope.condiments = [{ id: 1, name: "item1", price: 100 },
                             { id: 2, name: "item2", price: 200 },
                             { id: 3, name: "item3", price: 300 }];
        angular.forEach($scope.condiments, function(val, index) {
            $scope.selected[val.id] = "true";
            $scope.condiments[index].checked = true;
        })
        $scope.checked = function(key, status) {
            if (status == "true") {
                $scope.condiments[key].checked = true;
            } else {
                $scope.condiments[key].checked = false;
            }
        }
    }
于 2016-04-29T07:14:43.747 回答
1

我也面临这种问题,我通过这样使用解决了这个问题,

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

这将直接在 json 中更改您的 ng-checked True .. 所以在那之后简单地安慰您 API "condiments"您将得到您的答案..

如果你想设置默认检查 False . 只需使用 , ng-init="condiment.checked=0". 所以,如果你检查它会返回 True 否则你会得到 False 。

于 2016-04-29T06:46:13.170 回答
1

您可以通过复选框https://docs.angularjs.org/api/ng/directive/ngChecked浏览 Angular 文档。它清楚地告诉不要使用ng-modelwith ng-checked,它可能会导致意想不到的结果。

你可以删除ng-checked...

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

在控制器中,您可以获得调味品 id

$scope.checkItems = function(){
    var CID = [];
    angular.forEach($scope.condiments,function(condiment){
        if(condiment.checked){
           CID.push(condiment.id);
        }
    });

    //send CID to server
};
于 2016-04-29T06:57:25.583 回答