简体   繁体   English

$ http AngularJS之后数据范围未更新

[英]Scope of Datas not updating after a $http AngularJS

I can't understand why it does not update the $scope.user_free_status when I set a user free but when I unset the parameter it works perfectly. 我无法理解为什么我将用户设置为免费时为什么它不会更新$ scope.user_free_status,但是当我取消设置参数时它却可以正常工作。 I need to reload page in one case and not the other... The datas fetched are stored in the localstorage. 我需要在一种情况下而不是另一种情况下重新加载页面...提取的数据存储在localstorage中。

Here is the code: 这是代码:

    .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'InitialCtrl',
    resolve: {
      theUserFreeStatus: function(DataService) {
        return DataService.getUserFreeStatus();
      }
    }
  })

Controller: 控制器:

.controller('InitialCtrl', function($scope, $state, DataService ,FreeService, SharedService, theUserFreeStatus) {
// Showing set free but not unset or not
  if (FreeService.isSetFree()) {
    $scope.showSetFree    = false;
    $scope.showUnSetFree  = true;
  } else {
    $scope.showSetFree    = true;
    $scope.showUnSetFree  = true;
  }

  // Show the Free status set when arriving on page/app
  $scope.user_free_status = theUserFreeStatus;
  // Set user as Free
  $scope.setFree = function(activity, tags) {
    FreeService.setFree(activity, tags).success(function() {
      console.log($scope.user_free_status);
      $scope.user_free_status = DataService.getUserFreeStatus();
      console.log($scope.user_free_status);
      $scope.showSetFree    = false;
      $scope.showUnSetFree  = true;
      SharedService.goHome();
    })
  }  

  //// Free status unset
  $scope.unsetFree = function() {
    FreeService.unsetFree().success(function() {
      $scope.user_free_status = [];
      $scope.showSetFree    = true;
      $scope.showUnSetFree  = false;
      SharedService.goHome();
    });
  };
})

The services: 服务内容:

.factory('FreeService', function(WebService, $localstorage, $ionicPopup, DataService, $sanitize, CSRF_TOKEN) {
    var cacheFreeStatus = function(free_status) {
        $localstorage.setObject('user_free_status', free_status)
    };
    var uncacheFreeStatus = function() {
        $localstorage.unset('user_free_status')
    }
    return {
        setFree: function(activity, tags) {
            var status  = { SOME STUFF BLABLABLA };
            var setFree = WebService.post('setstatus/', sanitizeStatus(status));
            setFree.success(function(response) {
                console.log('available' + response.flash);
                cacheFreeStatus(response.status_response);
            })
            setFree.error(freeError)
            return setFree;
        },
        unsetFree: function() {
            var details  = {OTHER STUFF};
            var unsetFree = WebService.post('unsetstatus/', details);
            unsetFree.success(function(response) {
                console.log('unset ' + response.flash);
                uncacheFreeStatus(response.status_response);
            })
            unsetFree.error(freeError)
            return unsetFree;

        },
        isSetFree: function() {
            return $localstorage.get('user_free_status');
        }
    }
})
.service('DataService', function($q, $localstorage) {
  return {
    activities: $localstorage.getObject('activities'),
    getActivities: function() {
        return this.activities;
    },
    user_free_status: $localstorage.getObject('user_free_status'),
    getUserFreeStatus: function() {
        return this.user_free_status;
    }
  }
})
 * Local Storage Service
 ------------------------------------------------------*/
.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    unset: function(key) {
      localStorage.removeItem(key);
    },
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue;
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key] || '{}');
    }
  }
}])

When setting the user's status, the console returns that the $http call worked but an empty array for the $scope variable I try to set. 设置用户状态时,控制台返回$ http调用有效,但我尝试设置的$ scope变量为空数组。 Once I reload the page I can see the updates displayed. 重新加载页面后,我可以看到显示的更新。 If I unset the user's status, the $scope is properly updated without need to reload the page. 如果取消设置用户状态,则$ scope会正确更新,而无需重新加载页面。 The Webservice is just the $http call. Webservice只是$ http调用。

What am I missing here to have the $scope.user_free_status updated correctly without having to reload the page?? 我在这里缺少什么才能正确更新$ scope.user_free_status而不必重新加载页面?

Thanks for your time! 谢谢你的时间!

您的数据服务已作为服务注入,但您尚未将函数附加到此服务上,而是像在工厂中一样将其作为文字的一部分返回了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM