Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Angular returning promise object from service. Trying to access value to change ng-if directive

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!