Question:
I'm using ANGULARJS/UI-ROUTES , and currently my application only has the email after logging in, I need the logged in user ID to also come. The issue of routes I can get, the problem is that he doesn't see the ID to be able to bring it. I believe the problem could be with my claims ?
follows code.
This is where I pass the id (which is null, because "user" only sees the email)
<li ng-show="user"><a ui-sref="perfil({userId:user.id})">OLÁ, <em>{{ user }}</em></a></li>
Here is my controller that I try to get the id of the parameter I pass, but it only comes null because the user.id is null
angular.module('scases').controller('PerfilCtrl', PerfilCtrl);
PerfilCtrl.$inject = ['$scope', 'UserFactory', '$stateParams'];
function PerfilCtrl($scope, UserFactory, $stateParams) {
$scope.id = $stateParams.userId;
My real question is: Where am I going to get the user id logged in, if when I log in, only the email comes?
Answer:
Try using this ui-sref="perfil({userId: '{{user.id}}'})"
. Check if this state waits for the userId parameter.
.state('perfil', { url: '/perfil', templateUrl: 'perfil.html', controller: 'Perfil', params: { userId: null } })
To get the user id, you must create a service that returns the data you need. After obtaining these data, store them in the localstorage and create a service to make this data available to you.
Example:
angular.module('app').service('UsuarioService', UsuarioService);
UsuarioService.$inject = ['Restangular'];
function UsuarioService(Restangular) {
var service = {
getUsuario: function () {
var promise = Restangular.one('usuarios').get();
promise.then(function (response) {
window.localStorage.setItem('usuario', JSON.stringify(response.data.plain()));
return response.data;
});
return promise;
},
getUsuarioFromLocalStorage: function () {
return JSON.parse(window.localStorage.getItem('usuario'));
}
};
return service;
}