2015-01-14 50 views
0

永恆循環我目前正在Safari IPhone 4上測試我的應用程序。Chrome在此設備上正常工作,但safari已停止工作$ window.localStorage.setItem('token' ,令牌)行。沒有任何事情發生,應用程序沒有停下來,只是停止在這條線上工作。我認爲是無止境的循環,但也許有一些antoher troube。我已經閱讀了關於這個主題的另一個話題 - 但他們沒有幫助。

文件:

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

回答

0

而不是使用這個的:

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

我用:

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

而且這個工作