2015-05-31 26 views
2

我想在保存新對象後向表中添加一個新行。 的例子如下:AngularJS RESTful Web服務 - 無刷新地將行添加到表

var demoApp = angular.module("demoApp", ["ngResource"]); 

// Controller 
demoApp.controller("saveCategoryController", function($scope, saveCategoryService, categoriesService){ 
    $scope.categories = categoriesService.query(); 

    $scope.createCategory = function(){ 
     saveCategoryService.saveCategory($scope.category); 
     // I want to avoid this method to refresh my table. 
     // $scope.categories = categoriesService.query(); 

    };  
}); 

// Factory 
demoApp.factory("saveCategoryService", function($resource){ 
    return $resource("/demopro/saveCategory", {}, { 
     saveCategory: { 
      method: "POST" 
     } 
    }); 
}); 

demoApp.factory("categoriesService", function($resource){ 
    return $resource("/demopro/categories", {}, { 
     listAllCategories : { 
      method: "GET", 
      isArray: true 
     } 
    }); 
}); 

有沒有辦法做到這一點無需再作查詢到我的數據庫,以示我的表中的新行?

回答

2

是的,在邏輯上,你會回報保存,並將其添加到當前列表中保存的對象:

$scope.createCategory = function(){ 
     var newCategory = saveCategoryService.saveCategory($scope.category); 
     $scope.categories.push(newCategory);  
}; 
+0

它的作品,謝謝哥們。 – wilson

+0

你能幫我解決這種類似的情況嗎?謝謝。 http://stackoverflow.com/questions/30573582/angularjs-restful-web-service-delete-row-from-table – wilson

+0

我喜歡@andrunix第二個答案。使用您在刪除調用中使用的標識,並根據該標識刪除集合中的對象。 –