Hi i am creating an app using ionic side-menu in that i need to remember the logged in user for that i am saving the username and password like this.
window.localStorage['rem'] = 'yes';
window.localStorage['Username'] = $scope.loginData.username;
window.localStorage['password'] =$scope.loginData.password;
The saving function is working fine, problem is i tried to read the saved data while starting the app controller is not calling.How can i call the controller while loading the login page in ionic side menu app?
Thanks in advance
To save the data in local storage
window.localstorage.setItem ("username",$scope.loginData.username);
window.localStorage.setItem("password", $scope.loginData.password);
To retrieve data use
window.localstorage.getItem("username");
window.localstorage.getItem("password");
Now create a deviceready event listener inside $ionicPlatform.ready function.
check the sample code
$ionicPlatform.ready(function() {
document.addEventListener("deviceready", function()
{
if(window.localstorage.getItem("username")! == null && window.localstorage.getItem("password")! == null){
state.go("afterLogin.html");
}
else
state.go("login.html");
}, false);
}
Try this:
angular.module('ionic.utils', [])
.factory('$localstorage', ['$window', function($window) {
return {
set: function(key, value) {
$window.localStorage[key] = value;
},
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] || '{}');
}
}
}]);