duanjie6912 2017-07-20 21:03
浏览 37
已采纳

Ionic1工厂更新并返回更改

I have an ionic1 application which uses a factory / service.

.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){

            });

        }

    }

})

In my controller I'm calling the service on page load to show the categories, if no categories are found in the service it then checks the API and updates the categories in the service.

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

After updating, the user may want to add another category. After submitting to my API the controller calls the getCategories() function again but this isn't updating the categories currently in the DOM. I have to refresh the page to get the latest categories?

$scope.storeCategory = function()
  {

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

  }

for reference here's my entire controller

.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();

  }

});

Can anyone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • dsf6565 2017-07-20 21:11
    关注
    $scope.storeCategory = function()
      {
    
        expenses.storeCategory($scope.form);
        $scope.data.expenses = expenses.getCategories();
        $scope.closeModal();
    
      }
    

    change to

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

    The storeCategory must be a promise, and the logic to reload the infor of categories must be when success those promise, hope helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)