angularjs 删除具有范围数组的行会留下空白页

pod7payv  于 2022-11-21  发布在  Angular
关注(0)|答案(1)|浏览(151)

在从数据库中删除一行后,尝试用范围数组刷新视图,但没有成功:

$scope.remove = function() {
        var x = document.getElementById("name").textContent;
          $cordovaSQLite.execute(db, 'DELETE from table WHERE name=?', [x])
        .then(function(res) {
            var index = $scope.listItems.indexOf(x);
            $scope.listItems.splice(index, 1); 
        }, function(error) {
              console.log(error.message);
        })
    };

删除成功,但留下一个空白页面。要在我的Ionic应用程序中刷新,我必须转到另一个页面,然后返回:

$scope.showConfirm = function () {
    var x = document.getElementById("name").textContent;
    var confirmPopup = $ionicPopup.confirm({
        title: 'Deleting Journal Entry',
        template: 'Are you sure you want to delete this item ?'
    });
    confirmPopup.then(function (res) {
        if (res) {
            var query = "DELETE FROM chocolates where name = ?";
            $cordovaSQLite.execute(db, query, [x]);
            $state.go($state.current, $stateParams, {reload: true, inherit: false});
            console.log(x);
        } else {
            console.log('You are not sure');
        };
    });
};

这段代码也成功地删除了行,但是刷新功能不起作用。我尝试了alternative 1alternative 2的答案。

9jyewag0

9jyewag01#

if (res) {
                $ionicPlatform.ready(function () {
                    $scope.chocolates = [];
                    var query = "DELETE FROM chocolates where name = ?";
                    $cordovaSQLite.execute(db, query, [x]);
                    $cordovaSQLite.execute(db, 'SELECT * FROM chocolates ORDER BY choc_id DESC LIMIT 1', []).then(function (res) {
                        if (res.rows.length > 0) {
                            for (var i = 0; i < res.rows.length; i++) {
                                $scope.chocolates.push(res.rows.item(i));
                            }
                        }
                    }, function (err) {
                        console.error(err);
                    });
                    $scope.$apply();
                });
            } else {
                console.log('You are not sure');
            };

这就是解决办法。

相关问题