gpt4 book ai didi

javascript - 当复选框为 True 时,删除 Angular 内置数组中的对象

转载 作者:行者123 更新时间:2023-12-03 10:07:15 24 4
gpt4 key购买 nike

我需要循环检查列表并删除数组中已完成的项目,我目前拥有的代码是:

 app.controller("MainController", ["$scope",function($scope) {
$scope.list = [{
text: 'Figure your stuff out',
done: false
}, {
text: 'Count to seven',
done: false
}


];


$scope.addTask = function() {
$scope.list.push({
text: $scope.fromListText,
done: false
});
$scope.fromListText = '';
};
$scope.removeCompleted = function(index) {
$scope.list.splice(index, 1);

}

对于我的 Controller ,这是我的 Html:

    <html>

<head>
<script data-require="angular.js@1.4.0-rc.2" data-semver="1.4.0-rc.2" src="https://code.angularjs.org/1.4.0-rc.2/angular.js"></script>
<script data-require="angular-route@*" data-semver="1.4.0-rc.2" src="https://code.angularjs.org/1.4.0-rc.2/angular-route.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
<script src="script.js"></script>

</head>

<body ng-app="toDoList">
<div class="header">
<div class="container">
<h1>Your list brah</h1>
</div>
</div>
<div class="myList" ng-controller="MainController">
<div class="container">

<div ng-repeat="task in list">

<form class = "myTask">
Task:
<br>
<input type="checkbox" ng-model = "task.done">{{ task.text }}
</form>


</div>
<form>
<input placeHolder="Things I dont want to do..." type="text" ng- model="fromListText" ng-model-instant/>
<button class = "myButton" ng-click = addTask() >Add task</button>
</form>

<button class = "myButton" ng-click = "removeCompleted()" >Clear Completed Tasks</button>
</div>
</div>



</body>

</html>

我对脚本编写有点陌生,所以我想做的就是检查一下该复选框是否为真,如果是,则将其删除,如果您想查看其中的代码,请使用 plunkr 链接行动。 Plunkr Link

最佳答案

您在removeCompleted方法中想要的是

var $scope = {}

$scope.list = [{
text: 'Figure your stuff out',
done: true
}, {
text: 'Count to seven',
done: false
}, {
text: 'Count to eight',
done: true
}, {
text: 'Count to nine',
done: false
}
]

for(var i = 0; i < $scope.list.length; i++) {
if($scope.list[i].done) {
$scope.list.splice(i, 1)
i--
}
}

$scope.list.forEach(function(item) {
alert(item.text + ', ' + item.done)
})

这将循环遍历您的列表项,如果done为true,那么它将从数组中删除该项目

关于javascript - 当复选框为 True 时,删除 Angular 内置数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30305700/

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