离子应用程序中的无限观察者rootcope错误

时间:2015-10-13 19:43:15

标签: angularjs ionic

我有这个观察者:

  $scope.bool = false;
  $scope.$watch(function(){
    return sessionService.get('computed');
    }, function(){
      if (sessionService.get('computed')) {
        $scope.bool = true;
      } else {
        $scope.bool = false;
      }
  });

此离子应用程序menu.html中的此按钮:

<button nav-direction="back" class="button yy" ui-sref="app.result" ui-sref-active="currentNav" ng-show="bool" ng-click="navResult()">
      Board
    </button>

这是sessionService:

.service('sessionService', ['$cookieStore', function ($cookieStore) {
    var localStoreAvailable = typeof (Storage) !== "undefined";
    this.store = function (name, details) {
        if (localStoreAvailable) {
            if (angular.isUndefined(details)) {
                details = null;
            } else if (angular.isObject(details) || angular.isArray(details) || angular.isNumber(+details || details)) {
                details = angular.toJson(details);
            };
            sessionStorage.setItem(name, details);
        } else {
            $cookieStore.put(name, details);
        };
    };

    this.persist = function(name, details) {
        if (localStoreAvailable) {
            if (angular.isUndefined(details)) {
                details = null;
            } else if (angular.isObject(details) || angular.isArray(details) || angular.isNumber(+details || details)) {
                details = angular.toJson(details);
            };
            localStorage.setItem(name, details);
        } else {
            $cookieStore.put(name, details);
        }
    };

    this.get = function (name) {
        if (localStoreAvailable) {
                return getItem(name);
        } else {
                return $cookieStore.get(name);
        }
    };

    this.destroy = function (name) {
        if (localStoreAvailable) {
            localStorage.removeItem(name);
            sessionStorage.removeItem(name);
        } else {
            $cookieStore.remove(name);
        };
    };

    var getItem = function (name) {
        var data;
        var localData = localStorage.getItem(name);
        var sessionData = sessionStorage.getItem(name);

        if (sessionData) {
            data = sessionData;
        } else if (localData) {
            data = localData;
        } else {
            return null;
        }

        if (data === '[object Object]') { return null; };
        if (!data.length || data === 'null') { return null; };

        if (data.charAt(0) === "{" || data.charAt(0) === "[" || angular.isNumber(data)) {
            return angular.fromJson(data);
        };

        return data;
    };

    return this;
}])

在某个时刻,应用会创建变量computed并将其存储到sessionStorage中。

由于守望者,我得到了一个infite周期错误。 你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

“在某个时刻,应用程序会创建计算出的变量并将其存储到sessionStorage.

如果是这种情况,请在$rootScope

上触发一个事件
$rootScope.$broadcast('computedChanged');

然后在你的控制器中有$watch

$scope.$on('computedChanged', function(){
    ...
});