I've seen similar issues to the one but hoping someone can give me some help.
I'm trying to verify that a user is logged in and modify the html for a sign in or log out button.
this.getUser = function(){
return $http.get('/api/user');
};
service
vm.user = function() {
dataService.getUser().then(function(res){
if(res.data.userId.length){
return true;
}else{
return false;
}
});
}
controller
<li class="nav-item" ng-hide='vm.user()'>
<a class="nav-link" href="/api/signin">Sign in</a>
</li>
I've tried different variations of this to no avail. I'm either returning a promise object or not able to use the variable outside of promise. Thanks
Just use variable instead watching entire function in ng-hide directive. Please find the code snippet.
Controller File:
vm.flags = {};
vm.user = function() {
dataService.getUser().then(function(res){
if(res.data.userId.length){
vm.flags.isUserLoggedIn = true;
}else{
vm.flags.isUserLoggedIn = false;
}
});
}
Html File
<li class="nav-item" ng-hide='vm.flags.isUserLoggedIn'>
<a class="nav-link" href="/api/signin">Sign in</a>
</li>