-2

我需要一些有关此代码的帮助,我正在存储“活动”并将其删除。现在它正在存储但不删除。(我只需要这个代码的简单解决方案)。

Angular.js

angular
.module("TodoList",["LocalStorageModule"])
.factory("TodoService", function(localStorageService){

    var todoService = {};
    todoService.key = "angular-todolist";

    if(localStorageService.get(todoService.key)){
        todoService.activities = localStorageService.get(todoService.key);
    } else {
        todoService.activities = [];
    }

    todoService.add = function(newActv){
        todoService.activities.push(newActv);
        todoService.updaLocalStorage();
    };

    todoService.updaLocalStorage = function(){
        localStorageService.set(todoService.key, todoService.activities);
    };

    todoService.clean = function(){
        todoService.activities = [];
        todoService.updaLocalStorage();
    };

    todoService.getAll = function(){
        return todoService.activities;
    };

    toDoService.removeItem = function (item) {   **Creating function for remove**
        toDoService.activities = toDoService.activities.filter(function (activity) {
            return activity !== item;
        });
        toDoService.updateLocalStorage();
        return toDoService.getAll();
    };

    return toDoService;
})
.controller("TodoListCtrl", function($scope, todoService){

    $scope.todo = todoService.getAll();
    $scope.newActv = {};
    $scope.addActv = function(){
        todoService.add($scope.newActv);
        $scope.newActv = {};
    }
    $scope.removeActv = function (item) {     **Scope for remove**
        $scope.todo = ToDoService.removeItem(item);
    }
    $scope.clean = function(){
        todoService.clean();
    }
});

html

<!DOCTYPE html>
<html ng-app = "TodoList">
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
    <script src = "todoctrl.js"></script>
    <script src = "angular-local-storage.min.js"></script>
</head>
<body ng-controller = "TodoListCtrl">
    <ul>
        <li ng-repeat = "actividad in todo">
            {{actividad.descripcion}} -
            {{actividad.fecha | date: 'short'}} -
            <a href="#" ng-click = "removeActv(actividad)" style="color:red">x</a>  **Where is removing**
        </li>
    </ul>
    <form ng-submit = "addActv()">
        <input type="text" ng-model = "newActv.descripcion">
        <input type="datetime-local" ng-model = "newActv.fecha"> 
        <input type="submit" value = "Guardar">
    </form>
    <button ng-click = "clean()">Limpiar</button>
</body>
</html>
4

1 回答 1

0

更新你的ToDoService.removeItem(item);功能。如果要从活动array.splice(item)中删除项目,则用于从数组中删除项目。您想删除项目ToDoService.removeItem(item);功能,但您的功能没有这样做。它返回一个真或假的值。改变你的功能体。

于 2017-03-13T17:22:00.490 回答