angularjs - show and hide element after executing a function -


i need when user logged in system log out button appear , when logged out button disappear implement i'd first try button click following code:

$scope.test = function () {            if ($rootscope.authenticated) {              $rootscope.authenticated = false;          } else {                $rootscope.authenticated = true;          }      }
<li class="dropdown" id="profile-messages" ng-class="authenticated ? 'show' : 'hide' " @*ng-show="authenticated"*@>                  <a title="" href="#" data-toggle="dropdown" data-target="#profile-messages" class="dropdown-toggle">                      <i class="icon icon-user"></i>                      <span class="text" ng-model="username">{{username}}</span>                      <b class="caret"></b>                  </a>                  <ul class="dropdown-menu">                      <li><a href="#"><i class="icon-user"></i> {{user.authenticated}}</a></li>                      <li class="divider"></li>                      <li><a href="#"><i class="icon-check"></i> tasks</a></li>                      <li class="divider"></li>                      <li><a href="#" ng-show="user.authenticated" ng-click="logout()"><i class="icon-key"></i> خروچ</a></li>                  </ul>              </li>  <li class=""><a title="" href="#" ng-click="logout()" ng-show="authenticated"><i class="icon icon-share-alt"></i> <span class="text">logout</span></a></li>    <input type="button" id="test" value="test" ng-click="test()" />

and when click on button every thing worked fine when done same way in log in function not worked im using following code

    $scope.login = function () {          var haserror = false;          if (!haserror) {              if (!$rootscope.authenticated) {                  var result = loginfactory.adminlogin($scope.loginform);                  result.then(function (result) {                      if (!result.ok) {                          toastr.warning(result.message, 'اخطار', { timeout: 7000 });                      } else {                          $location.path(result.url);                          $rootscope.authenticated = result.authenticated;                          $rootscope.username = result.user;                                                                         }                  })              } else {                  toastr.error("you logged in!", '', { timeout: 0 });              }          }

and service is:

adminlogin: function (model) {                var deferredobject = $q.defer();              $http.post('/core/user/loginapp', { model: model }).success(function (data) {                    if (!data.ok) {                      deferredobject.resolve({ ok: false, message: data.message });                  } else {                      deferredobject.resolve({ ok: true, url: data.url, user: data.user, authenticated: data.authenticated });                  }              }).error(function () {                  deferredobject.resolve({ success: false, message: data.message });              });              return deferredobject.promise;          }

how can solve problem

use double negation make boolean.

$rootscope.authenticated = !! result.authenticated; 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -