如何修复我的书签添加删除功能

时间:2019-06-13 08:29:58

标签: angularjs

我正在设置书签服务。现在,我得到奇怪的结果,例如双存储值和默认值。

这是一个用于存储文档ID的Angular应用程序。

HTML

<a ng-click="bookmarkResult(result)">add</a>
<a ng-click="removeBookmark(bookmark)">remove</a>

我的功能

 $scope.bookmarkResult = function (result) {
            bookmarkService.addBookmark(result);
       };

  $scope.removeBookmark = function(bookmark) {
    bookmarkService.removeBookmark(bookmark);
    // sends false
    console.log(bookmarkService.removeBookmark(bookmark));
  };

bookmark.js服务

'use strict';

angular.module('app.bookmarkService', ['ngRoute'])
    .service('BookmarkService', ['localStorageService', function (localStorageService) {

        var key = 'fud_bookmarks';

        this.addBookmark = function (resultItem) {
            var bookmarks = this.getBookmarks();
            bookmarks.push(resultItem);
            console.log(bookmarks.push(resultItem));
            return setBookmarks(bookmarks);
        };

        this.removeBookmark = function (resultItem) {
            var bookmarks = this.getBookmarks();
            var i = bookmarks.indexOf(resultItem);
            if (i >= 0) {
                bookmarks.splice(i, 1);
                console.log(bookmarks);
                return setBookmarks(bookmarks);
            }
            return false;
        };

        this.getBookmarks = function () {

            return getBookmarks();
        };

        this.getBookmarkCount = function() {
            return getBookmarks().length;
        };

        function getBookmarks() {
            var bookmarks = localStorageService.get(key);
            if (!bookmarks) {
                bookmarks = [];
            }
             console.log(bookmarks);
            return bookmarks;
        }

        function setBookmarks(bookmarks) {
            console.log(bookmarks);
            return localStorageService.set(key, bookmarks);
        }

    }]);

当我单击添加ID时,它将添加其中两个。 删除根本不起作用。

我怎么了。

0 个答案:

没有答案