Hi!
Ok I’m stuck with that for a while now, thought I could leave it for later but haven’t found my answer even when following the tutorials…
Short story: I can’t display the object datas I have pulled in
Here is the story:
1/ User arrives on log in page > log in > call to API > success returns the user’s data, logs him in and stores his data & session locally.
2/ User directed to the home page via a state.go(‘app.home’).
3/ My state app.home has a resolve to pull in the user datas from the local storage. Following the tutorial concept here. See my code below:
// Home when logged in
.state('app.home', {
url: "/home",
resolve: {
user: function(DataService) {
return DataService.getUser()
}
},
views: {
'menuContent': {
templateUrl: "templates/home.html",
controller: 'HomeCtrl'
}
}
})
the Service:
/*
* Shared Data Service for the views
------------------------------------------------------*/
.service('DataService', function($q, $localstorage) {
return {
user: $localstorage.getObject('user'),
getUser: function() {
return this.user
}
}
})
Then the controller:
/*
* Home Controller
------------------------------------------------------*/
.controller('HomeCtrl', function($scope, $state, user) {
// User's info
$scope.user = user;
//FOR DEV PURPOSE
console.log(user);
console.log(user.username)
})
At last but not least the html
<ion-view view-title="">
<ion-content>
<div class="container">
<h1>{{user.username}}</h1> ETC......
4/ When being redirected from login, this home page displays nothing and the console logs:
> Object {}
>undefined
Meaning user is empty…
5/ I reload the page page and now the console logs:
> Object {id: 2, username: "test001", created_at: "2015-01-23 16:49:24", updated_at: "2015-01-23 19:46:43", email: "test001@test.com"…}code: ""created_at: "2015-01-23 16:49:24"email: "test001@test.com"id: 2ip: "192.168.10.1"password_temp: ""salt: ""updated_at: "2015-01-23 19:46:43"username: "test001"__proto__: Object
> test001
and in the nav-view I can see the name (good news…) but I added the {{user.username}} in the ion nav bar but this will not show the username no matter how many times I reload…
6/ When I log out (clears all the local storage and logs out) and log in with a new user, I see the previous user’s username after redirection to the home page. Once I reload I see mine appear…
What’s going on? What I am not doing correctly?
Thank you sooo much for taking the time to read & help!!! I owe anyone helping a beer!!