gpt4 book ai didi

javascript - AngularJS 复选框无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:26:36 25 4
gpt4 key购买 nike

我正在创建一个简单的 angularJs为我自己的学习目的而申请。我附上了下面的代码片段。我有两个JSON数据。

一个代表杂货商品的集合,另一个代表用户选择的商品。如果所选数据与收集数据相匹配,我会将复选框标记为已选中。我可以在单击复选框时插入数据,但如果我随机开始取消选中,它就无法正常工作。如果您能帮助我纠正我犯的错误,我将不胜感激。另外,如果有任何简单的方法来比较 JSON,我将不胜感激。数据。我在这里使用 name比较。有没有其他简单的方法来比较 JSON 中的单个对象? , 没有提到 nametype正如我在示例中所做的那样,在 angularJs 中或 Javascript

(function() {
angular.module("groceryApp", []).controller("groceryController", groceryControllerFunction);
groceryControllerFunction.$inject = ["$scope", "$filter"];

function groceryControllerFunction($scope, $filter) {
$scope.groceryCollection = groceryCollection_JSON;
$scope.selectedItems = selectedItems_JSON;
$scope.toggleSelection = function(item) {
var isRemoved = false;
if ($scope.selectedItems.length > 0) {
for (var i = 0; i < $scope.selectedItems.length; i++) {
if ($scope.selectedItems[i].name.indexOf(item.name) > -1) {
$scope.selectedItems.splice($scope.selectedItems.indexOf(item), 1);
isRemoved = true;
break;
}
console.log("Checking..." + $scope.selectedItems[i].name.indexOf(item.name));
}
}
// else {
if (!isRemoved) {
$scope.selectedItems.push(item);
}
// }
console.log($scope.selectedItems)
}
$scope.addCustomItem = function() {}
}
var groceryCollection_JSON = [{
"name": "Tomato",
"type": "Roma"
}, {
"name": "Cauliflower",
"type": "Organic"
}, {
"name": "Apple",
"type": "Gala"
}, {
"name": "Orange",
"type": "Sweet n' Sour"
}, {
"name": "Grapes",
"type": "Wild"
}];
var selectedItems_JSON = [{
"name": "Tomato",
"type": "Roma"
}];
})();
<!DOCTYPE html>
<html ng-app="groceryApp">

<head>
<meta charset="utf-8">
<meta name="description" content="First Angular App">
<meta name="keywords" content="HTML, Javascript, AngularJs">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.angularjs.org/1.6.1/angular.min.js"></script>
<script src="app.js"></script>
<title>A simple AngularJs Application</title>
</head>

<body ng-controller="groceryController">
<h1>Welcome to my Grocery Store</h1>
<p>Select your items from the options below.</p>
<div ng-repeat="item in groceryCollection">
<input type="checkbox" id="{{item.name}}" value="{{item.name}}" ng-checked="selectedItems[$index].name.indexOf(item.name)>-1" ng-click="toggleSelection(item)">
<label for="{{item.name}}">{{item.name}}</label>
<input type="number" step="1" min="0" max="5" placeholder="Quantity" />
</div>
<p>
<input type="checkbox" id="others">
<label for="others">Others</label>
</p>
<p>Please Enter your item if not displayed in the list above:</p>
<p id="extraItems"> <span><input type="text" placeholder="Please Enter your Item" ng-model="customItem"/></span> <span><input type="text" placeholder="Please Enter your Item Type" ng-model="customItemType"/></span> <span><input type="number" step="1" min="0" max="5" placeholder="Quantity"/></span>
</p>
<p>
<input type="button" value="Add Item" ng-click="addItem();" />
</p>
<p> <a href="#">Add more items</a>
</p>
<div>
<p>Your selected items:</p>
<table>
<tr>
<th>Name</th>
<th>Type</th>
</tr>
<tr ng-repeat="selection in selectedItems">
<td>{{selection.name}}</td>
<td>{{selection.type}}</td>
</tr>
</table>
</div>
</body>

</html>

最佳答案

您可以只在 groceryCollection 中的项目上使用 selected 标志。

这摆脱了 toggleSelection 即减少到 ng-click="item.selected = !item.selected"

而 ng-checked 只是 item.selected

所选项目简单地过滤为 ng-repeat="selection in groceryCollection | filter: {selected:true}"

您所做的只是在这个选择过程中,我们只标记被选中的项目。

(function() {
angular.module("groceryApp", []).controller("groceryController", groceryControllerFunction);
groceryControllerFunction.$inject = ["$scope", "$filter"];

function groceryControllerFunction($scope, $filter) {
$scope.groceryCollection = groceryCollection;

$scope.addCustomItem = function() {}
}
var groceryCollection = [{
"name": "Tomato",
"type": "Roma"
}, {
"name": "Cauliflower",
"type": "Organic"
}, {
"name": "Apple",
"type": "Gala"
}, {
"name": "Orange",
"type": "Sweet n' Sour"
}, {
"name": "Grapes",
"type": "Wild"
}];

})();
<!DOCTYPE html>
<html ng-app="groceryApp">

<head>
<meta charset="utf-8">
<meta name="description" content="First Angular App">
<meta name="keywords" content="HTML, Javascript, AngularJs">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.angularjs.org/1.6.1/angular.min.js"></script>
<script src="app.js"></script>
<title>A simple AngularJs Application</title>
</head>

<body ng-controller="groceryController">
<h1>Welcome to my Grocery Store</h1>
<p>Select your items from the options below.</p>
<div ng-repeat="item in groceryCollection">
<input type="checkbox" id="{{item.name}}" value="{{item.name}}" ng-checked="item.selected" ng-click="item.selected = !item.selected">
<label for="{{item.name}}">{{item.name}}</label>
<input type="number" step="1" min="0" max="5" placeholder="Quantity" />
</div>
<p>
<input type="checkbox" id="others">
<label for="others">Others</label>
</p>
<p>Please Enter your item if not displayed in the list above:</p>
<p id="extraItems"> <span><input type="text" placeholder="Please Enter your Item" ng-model="customItem"/></span> <span><input type="text" placeholder="Please Enter your Item Type" ng-model="customItemType"/></span> <span><input type="number" step="1" min="0" max="5" placeholder="Quantity"/></span>
</p>
<p>
<input type="button" value="Add Item" ng-click="addItem();" />
</p>
<p> <a href="#">Add more items</a>
</p>
<div>
<p>Your selected items:</p>
<table>
<tr>
<th>Name</th>
<th>Type</th>
</tr>
<tr ng-repeat="selection in groceryCollection | filter: {selected:true}">
<td>{{selection.name}}</td>
<td>{{selection.type}}</td>
</tr>
</table>
</div>
</body>

</html>

关于javascript - AngularJS 复选框无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com