我试图在保存新对象后向表中添加新行。 示例如下:
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
}
});
});
有没有办法做到这一点,而无需再次查询我的数据库以显示表中的新行?
最佳答案
是的,从逻辑上讲,您将在保存时返回保存的对象并将其添加到当前列表中:
$scope.createCategory = function(){
var newCategory = saveCategoryService.saveCategory($scope.category);
$scope.categories.push(newCategory);
};
关于javascript - AngularJS RESTful Web 服务 - 将行添加到表中而不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563928/