Ionic/AngularJS 复选框和 ng-repeat 来自 API
Ionic/AngularJS checkbox and ng-repeat from API
我的想法是将真实的复选框值从移动设备发送到 API,这样我就可以用选定的调味品下订单,但我无法掌握它。调味品从对 API.
的 HTTP GET 调用返回
<ion-checkbox ng-repeat="condiment in condiments" ng-model="condiment.checked"
ng-checked="checkItem(condiment.id)">
{{condiment.name}}
{{condiment.price}}
</ion-checkbox>
它调用函数:
$scope.checkItem = function (id) {
return $scope.condiments[id-1].checked = true;
};
(负1是因为ID从1开始,数组从0开始)
但它不会在 checked/unchecked 时调用,而是默认检查我的所有资源,一旦我单击取消选中它们,没有任何变化。
属性 'checked' 不是调味品原始 JSON API 输出的一部分。它有 ID、名称和价格。
问题:
有没有更简单的方法将检查过的 ID 发送回服务器?
编辑:
我之前尝试过设置默认变量,但没有任何作用:
for(var i=0; i<$scope.condiments; i++){
$scope.condiments[i].checked = false;
}
我也遇到过这种问题,
我通过这样使用解决了这个问题,
<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"
。所以,如果你检查它会 return True 否则你会得到 False 。
您可以在复选框 https://docs.angularjs.org/api/ng/directive/ngChecked 上浏览 Angular 文档。它明确告诉不要将 ng-model
与 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
};
试试这个,这是为您创建的 fiddle。 see this
<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;
}
}
}
我的想法是将真实的复选框值从移动设备发送到 API,这样我就可以用选定的调味品下订单,但我无法掌握它。调味品从对 API.
的 HTTP GET 调用返回<ion-checkbox ng-repeat="condiment in condiments" ng-model="condiment.checked"
ng-checked="checkItem(condiment.id)">
{{condiment.name}}
{{condiment.price}}
</ion-checkbox>
它调用函数:
$scope.checkItem = function (id) {
return $scope.condiments[id-1].checked = true;
};
(负1是因为ID从1开始,数组从0开始) 但它不会在 checked/unchecked 时调用,而是默认检查我的所有资源,一旦我单击取消选中它们,没有任何变化。
属性 'checked' 不是调味品原始 JSON API 输出的一部分。它有 ID、名称和价格。
问题:
有没有更简单的方法将检查过的 ID 发送回服务器?
编辑:
我之前尝试过设置默认变量,但没有任何作用:
for(var i=0; i<$scope.condiments; i++){
$scope.condiments[i].checked = false;
}
我也遇到过这种问题, 我通过这样使用解决了这个问题,
<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"
。所以,如果你检查它会 return True 否则你会得到 False 。
您可以在复选框 https://docs.angularjs.org/api/ng/directive/ngChecked 上浏览 Angular 文档。它明确告诉不要将 ng-model
与 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
};
试试这个,这是为您创建的 fiddle。 see this
<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;
}
}
}