Safari在Angular的$ window.localStorage.setItem上进行永久循环

时间:2015-01-14 14:29:26

标签: javascript iphone angularjs loops safari

我目前正在Safari IPhone 4上测试我的应用程序.Chrome在此设备上正常运行,但safari已停止使用 $ window.localStorage.setItem('token',token)行。没有什么事情发生,应用程序没有下降,只是停止在这条线上工作。我认为是无限循环但也许有一些反对的麻烦。我已经阅读了关于这个主题的另一个主题 - 但他们没有帮助。

文件:

app.js

.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

  $stateProvider
  .state('home', {
    url: '/',
  })
  .state('signin', {
    url: '/signin',
    templateUrl: '/templates/signin.html',
    controller: 'SignInCtrl'
  })
  .state('eventmenu', {
    url: '/event',
    abstract: true,
    templateUrl: '/templates/event-menu.html',
  })
  .state('eventmenu.checkin', {
    url: '/console',
    views: {
    'menuContent' :{
      templateUrl: '/templates/console.html',
      controller: 'ConsoleCtrl'
    }
  },
  })
  .state('eventmenu.tasks', {
  url: '/tasks',
  views: {
    'menuContent' :{
      templateUrl: '/templates/tasks.html',
      controller: 'TasksCtrl'
    }
  },
  })
  .state('eventmenu.task', {
    url: '/task/:taskId',
    views: {
      'menuContent': {
       templateUrl: '/templates/task-detail.html',
       controller: 'TaskDetailCtrl'
    }
    }
  })
  .state('eventmenu.constructor', {
    url: '/constructor',
    views: {
    'menuContent': {
      templateUrl: '/templates/constructor.html',
      controller: 'TaskConstructorCtrl'
    }
   }
  });

  $urlRouterProvider.otherwise('/signin');
  $httpProvider.interceptors.push('AuthInterceptor');
});

services.js

.factory('authenticationSvc', function($http, $q, $window, $location, urls, $state,     $rootScope) {
    var userInfo = false;


    function login(userName, password) {
      var deferred = $q.defer();
      var data = {
        username: userName,
        password: password
      };
    $http({
      url: urls.login,
      method: 'POST',
      data:data
    }).then(function (response, status, headers, config) {

    if (response.data.token) {
      //problem starts here
      authenticationSvc.setToken(response.data.token);
      $state.go('eventmenu.tasks');

    }
    deferred.resolve(response, status, headers, config);
    }, function(response, status, headers, config) {
      deferred.reject(response, status, headers, config);
    });

    return deferred;
  }

  function getToken() {
    return $window.localStorage.getItem('token');
  }

  function setToken(token) {
    //problem occurs here. after this line alerts don't works 
    $window.localStorage.setItem('token', token);
  }

  function deleteToken() {
    $window.localStorage.removeItem('token');
  }

  function getUserInfo(){
    return userInfo;
  }

  var authenticationSvc = {
    login: login,
    getUserInfo: getUserInfo,
    deleteToken: deleteToken,
    getToken: getToken,
    setToken: setToken
  };

return authenticationSvc;

})

controllers.js

....
.controller('SignInCtrl', function($scope, $state, authenticationSvc) {
    $scope.user = {};
    $scope.signIn = function(user) {
    var result = authenticationSvc.login(user.username, user.password);
    result.promise.then(function(data){
        if(data.status == 400){
            $scope.errors = {}
            for (var key in data.data) {
            if (data.data.hasOwnProperty(key)) {
                $scope.errors[key] =  data.data[key].join(", ");
            }
            }
        }
    })
};
})
.....

1 个答案:

答案 0 :(得分:0)

而不是使用它:

function getToken() {
    return $window.localStorage.getItem('token');
}

我用过:

function getToken() {
    return localStorageService.get('token');
 }
 // import 'LocalStorageModule' dependence in module.

这很有效