在 angularjs 应用程序中构建我的控制器、服务和工厂的正确方法...服务或工厂?

Proper way to structure my controller, service, and factory in angularjs application...service or factory?

我一直在思考 "angularjs" 的思维方式 (Angular 1),并且在完成一个小型个人项目时,我的理解力相对较好。我遇到了一些障碍,不是因为我无法让它工作,而是我想知道在我的应用程序中设置数据的正确方法是什么。

基本情况是这样的:

我有 3 个 json 个文件:

categories.json
products.json
vendors.json

这些保存数据(稍后我将从数据库中获取,但现在正在简化)。

我基本上需要从这三个文件中加载数据,这样我就可以形成一个变量来保存所有 "Products"(这是我单独声明的 JS class)。

我首先将数据存储在一个控制器中(相关代码如下):

myApp.controller('productListController', ['$scope', '$http', '$q', function ($scope, $http, $q) {

var promises = [];
promises.push(getCategories($http));
promises.push(getVendors($http));
promises.push(getProducts($http));

$q.all(promises).then(function (response) {
  //categories = response[0];
  //vendors = response[1];
  //products = response[2];
  $scope.products = createProductList(response);
  $scope.vendors = response[1].data;
  $scope.vendorChecked = getCheckedVendors($scope.vendors);
})

这很好用,但我意识到我需要在其他视图中使用这些数据,这促使我尝试将此代码移到服务中。

我在执行此操作时遇到的问题是,我不知道如何让控制器知道服务已完成获取数据,以便我可以将其保存在 ProductListController $scope 中。

我需要有一种方法,例如:

myApp.service('ProductService', ['$http', '$q', function ($http, $q) {

self = this;
var promises = [];
promises.push(getCategories($http));
promises.push(getVendors($http));
promises.push(getProducts($http));

$q.all(promises).then(function (response) {
//These three I would like to update in ProductListController
//when it is done.
  self.products = createProductList(response);
  self.vendors = response[1].data;
  self.vendorChecked = getCheckedVendors(self.vendors);
})

这是正确的做法吗?如果是这样,我如何让控制器知道服务已完成获取数据并保存例如:

$scope.products = ProductService.products;
$scope.vendors = ProductService.vendors;
$scope.categories = ProductService.categories;

这是正确的方法吗?我想到的另一种方法是使用工厂而不是服务。然后我遇到了另一个问题,因为我有例如:

return {
    getProducts: function() {
         //http get request code in here
         return promise
    },
    getVendors: function() {
         //http get request code in here
        return promise
    },
    getCategories: function() {
         //http get request code in here
        return promise
    },
    getAllData: function () {
    //in here I want to use the three promises in the 3 functions above
    //but I am not able to call them from here. If I was able to do that
    //then I could call this method from ProductListController and get the
    //data that way.
    }

很抱歉,如果这很长,但我想描述我尝试过的不同的事情。我知道我可以让它发挥作用,但我想学习正确的方法,或一些正确的方法。

最好总是return承诺:

var promises = [];
promises.push(getCategories($http));
promises.push(getVendors($http));
promises.push(getProducts($http));

return $q.all(promises)

如果您也不满意在每个控制器中调用 createProductList、getCheckedVendors - 考虑将此转换为 $http transformResponce https://docs.angularjs.org/api/ng/service/$http。 或者您可以创建自己的承诺。 (使用 $q.defer https://docs.angularjs.org/api/ng/service/$q)。

其实用servie还是factory都无所谓。这是工厂:

var factory = {};
factory.getProducts: function() {
         return promise
}
factory.getCategories: function() {
         return promise
}
factory.getVendors: function() {
         return promise
}
factory.getAllData: function () {
    var promises = [];
    promises.push(factory.getProducts());
    promises.push(factory.getCategories());
    promises.push(factory.getVendors());

    return $q.all(promises)
}
return factory;

在控制器中,你只有: MyFactory.getAllData().然后(...)