Safari 在 Angular 的 $window.localStorage.setItem 上无限循环
Safari goes eternal loop on Angular's $window.localStorage.setItem
我目前正在 Safari 上测试我的应用程序 IPhone 4。Chrome 在此设备上运行良好,但 safari 已停止在 $window.localStorage.setItem('token', token) 行。什么都没发生,应用程序没有关闭,只是停止处理这个 line.I 认为是无限循环,但也许有一些麻烦。我已经阅读了关于此主题的其他主题 - 但它们没有帮助。
文件:
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(", ");
}
}
}
})
};
})
.....
而不是使用这个:
function getToken() {
return $window.localStorage.getItem('token');
}
我用过:
function getToken() {
return localStorageService.get('token');
}
// import 'LocalStorageModule' dependence in module.
这有效
我目前正在 Safari 上测试我的应用程序 IPhone 4。Chrome 在此设备上运行良好,但 safari 已停止在 $window.localStorage.setItem('token', token) 行。什么都没发生,应用程序没有关闭,只是停止处理这个 line.I 认为是无限循环,但也许有一些麻烦。我已经阅读了关于此主题的其他主题 - 但它们没有帮助。
文件:
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(", ");
}
}
}
})
};
})
.....
而不是使用这个:
function getToken() {
return $window.localStorage.getItem('token');
}
我用过:
function getToken() {
return localStorageService.get('token');
}
// import 'LocalStorageModule' dependence in module.
这有效