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


i've seen similar issues 1 hoping can give me help.

i'm trying verify user logged in , modify html 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 no avail. i'm either returning promise object or not able use variable outside of promise. thanks

just use variable instead watching entire function in ng-hide directive. please find 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> 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -