1

I am ng-include-ing a nav bar with a controller called NavCtrl which has a logout button that listens to a logout function and a logoutError field.

Both logout and logoutError are used only inside the template partial (nav.html). The partial doesn't use any scope variables from the parent or vice-versa.

The logout function works just fine on click, but the logoutError doesn't update to reflect a invalid logout response from the server. The scope variables work just fine on load, they just dont update the second time.

I cannot figure out why this is happening.

Code here:

nav.html

<div ng-controller="NavCtrl">
  <!-- Nav Bar Stuff -->
          <li><a href="#" ng-click="logout()" ng-hide="logoutRequestInProgress">Logout</a></li>
  <!-- More Nav Bar Stuff -->

  <!-- logoutError here doesn't update, but loads correctly when the page is first rendered -->
  <div class="alert alert-danger alert-dismissable" ng-show="logoutError">
    <strong>Logout Failed!</strong> {{ logoutError }} 
  </div>
</div>

nav.js

'use strict';

app.controller('NavCtrl', function ($scope, $window, $location, AuthService) {
  if ($window.localStorage.token) {
    $scope.logout = function () {    
      // Reset error vars
      $scope.logoutError = null;
      AuthService.logout().then(function() {
        // success stuff 
      }, function (response) {
        // !!! THIS UPDATE DOESNT WORK !!!
        $scope.logoutError = 'Server Error';
      });
    };
  }
});
nknj
  • 2,436
  • 5
  • 31
  • 45
  • Please read this: http://stackoverflow.com/questions/11412410/angularjs-losing-scope-when-using-ng-include – Kasyx Apr 29 '14 at 12:47
  • Thanks, I did read it but that question is trying to use a variable in the child scope from the parent scope. Its not what I am asking here. – nknj Apr 29 '14 at 13:34
  • Do you find a solution? I'm facing a similar problems. – xi.lin May 22 '14 at 04:40
  • Unfortunately, I did not. :( Please reply to this if you find a solution. – nknj May 22 '14 at 09:33

0 Answers0