溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例

發(fā)布時(shí)間:2020-10-21 04:53:05 來源:腳本之家 閱讀:157 作者:lqh 欄目:web開發(fā)

Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例

有人會(huì)說刪除這東西有什么可講的,寫個(gè)刪除的service,controller調(diào)用一下不就完了。

嗯...看起來是這樣,但是具體實(shí)現(xiàn)起來真的有這么簡單嗎?首先有以下幾個(gè)坑

怎么確定數(shù)據(jù)是否刪除成功?

怎么同步視圖的數(shù)據(jù)庫的內(nèi)容?

1.思路

1.實(shí)現(xiàn)方式一

刪除數(shù)據(jù)庫中對應(yīng)的內(nèi)容,然后將$scope中的對應(yīng)的內(nèi)容splice

2.實(shí)現(xiàn)方式二

刪除數(shù)據(jù)庫中對應(yīng)的內(nèi)容,然后再reload一下數(shù)據(jù)(也就是再調(diào)用一次查詢方法,這種消耗可想而知,并且還要保證先刪除數(shù)據(jù)再查詢)

2.具體實(shí)現(xiàn)方式

刪除數(shù)據(jù)的service:用異步,返回promise

service('deleteBlogService',//刪除博客
    ['$rootScope',
      '$http',
      '$q',
      function ($rootScope, $http, $q) {
        var result = {};
        result.operate = function (blogId) {
          var deferred = $q.defer();
          $http({
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            url: $rootScope.$baseUrl + "/admin/blog/deleteBlogById",
            method: 'GET',
            dataType: 'json',
            params: {
              id: blogId
            }
          })
            .success(function (data) {
              deferred.resolve(data);
              console.log("刪除成功!");
            })
            .error(function () {
              deferred.reject();
              alert("刪除失?。?)
            });
          return deferred.promise;
        };
        return result;
      }])

controller里面注意事項(xiàng)

要特別注意執(zhí)行順序:確保己經(jīng)刪除完成之后再去reload數(shù)據(jù),不然會(huì)出來視圖不更新             

 /**
         * 刪除博客
         */
        $scope.deleteBlog = function (blogId) {
          var deletePromise = deleteBlogService.operate(blogId);
          deletePromise.then(function (data) {
            if (data.status == 200) {
              var promise = getBlogListService.operate($scope.currentPage);
              promise.then(function (data) {
                $scope.blogs = data.blogs;
                $scope.pageCount = $scope.blogs.totalPages;
              });
            }
          });
        };

以上就是Angularjs的$http異步刪除數(shù)據(jù)的實(shí)例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI