Ionic1 出厂更新和 return 更改

Ionic1 Factory Update and return changes

我有一个使用工厂/服务的 ionic1 应用程序。

.factory('expenses', function($http) {

    var expenseCategories = [];

    return {

        getCategories: function(){

            return expenseCategories;

        },

        updateCategories: function(){

            return $http.get('URL_HERE').then(function(response){

                return expenseCategories = response.data.data[0];

            });

        },

        storeCategory: function(category){

            $http({
                method: 'POST', 
                data: category,
                url: 'URL_HERE',
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem("token")}
            }).success(function(data) {

                expenseCategories = data.data[0];

            }).error(function(error){

            });

        }

    }

})

在我的控制器中,我在页面加载时调用服务以显示类别,如果在服务中未找到类别,它会检查 API 并更新服务中的类别。

if(expenses.getCategories().length != 0){
      $scope.data.expenses = expenses.getCategories();
  } else {
      $scope.data.expenses = expenses.updateCategories();
  };

更新后,用户可能想添加另一个类别。在提交到我的 API 之后,控制器再次调用 getCategories() 函数,但这并没有更新当前在 DOM 中的类别。我必须刷新页面才能获得最新的类别吗?

$scope.storeCategory = function()
  {

    expenses.storeCategory($scope.form);
    $scope.data.expenses = expenses.getCategories();
    $scope.closeModal();

  }

我的整个控制器供参考

.controller('expenseCategoryCtrl', function($scope, $state, expenses, $ionicModal, $http) {

  $scope.data = {};
  $scope.form = {};

  if(expenses.getCategories().length != 0){
      $scope.data.expenses = expenses.getCategories();
  } else {
      $scope.data.expenses = expenses.updateCategories();
  };

  $scope.storeCategory = function()
  {

    expenses.storeCategory($scope.form);
    $scope.data.expenses = expenses.getCategories();
    $scope.closeModal();

  }

});

谁能指出我正确的方向?

$scope.storeCategory = function()
  {

    expenses.storeCategory($scope.form);
    $scope.data.expenses = expenses.getCategories();
    $scope.closeModal();

  }

改为

$scope.storeCategory = function()
  {

    expenses.storeCategory($scope.form).then(function(data){
        $scope.data.expenses = expenses.getCategories();
        $scope.closeModal();
    });
  };

storeCategory 必须是一个 promise,重新加载类别信息的逻辑必须是当这些 promise 成功时,希望能有所帮助。