javascript - How to remove checkbox from array in angular js -
trying create function remove selected checkbox items array can't seem figure out. have tried using splice , .pop() won't remove items select. think best way use if statement not know how write shows true or false. please help!!!
js:
.controller('todoctrl', function($scope, todofactory){ //set $scope variables $scope.tasks = todofactory.tasks; $scope.removetasks = todofactory.removetasks; }) .factory('todofactory', ['$http', function($http){ var todo = { tasks: [], removetasks: function(selectedtask){ angular.foreach(todo.tasks, function(value, selectedtask){ var = todo.tasks.indexof(value); todo.tasks.splice(todo.tasks.indexof(i), 1); }); } }; return todo; }])
html:
<button ng-click="removetasks()">remove</button>
i did not understood if wanted delete selected tasks or one. anyway, can example :
js:
app .controller('todoctrl', function($scope, todofactory){ //set $scope variables $scope.data = {}; $scope.data = todofactory.data; $scope.removetasks = todofactory.removetasks; $scope.removetask = todofactory.removetask; }) .factory('todofactory', ['$http', function($http){ var todo = { data : { tasks: [ {text: "hello world", done: false}, {text: "hello world2", done: false}, {text: "hello world3", done: false} ] }, removetasks: function(){ todo.data.tasks = todo.data.tasks.filter(function(task){ return !task.done; }); }, removetask: function(index){ todo.data.tasks.splice(index, 1); }, }; return todo; }]);
html:
<body ng-controller="todoctrl"> <div ng-repeat='task in data.tasks'> <input type='checkbox' ng-model='task.done' /> <span>{{task.text}}</span> </div> <br/> <button ng-click="removetasks()">remove</button> </body>
plunkr : https://plnkr.co/edit/btg0feuzl1uotift1nhc?p=preview
Comments
Post a Comment