Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda