title: angularjs - Watching service properties in a controller scope... changing them from a directive... no update?
body: <p>Let's say I have a very simple service with a few properties on it. If I use the service in a controller, put the service's properties on the scope so that they are bound to my view, and update them from the controller, they update in the view. This is the behavior I'd expect. However, if the same service's properties are modified from a directive outside of the controller's scope, the view is not updated (unless something triggers a watch to be updated in the controller's scope?). There is obviously something fundamental that I'm missing here, but search search searching has not led me to the answer. </p>
  
  <p>Here is an example on <a href="http://jsfiddle.net/SAsBa/45/" rel="nofollow">JSFiddle</a>. </p>
  
  <pre><code>app = angular.module('app', []);
  // simple service to track application's logon status
  app.factory('AuthService', function () {
      var status = {
          isLoggedIn: false
      };
  
      return {
          status: status,
          login: function () {
              status.isLoggedIn = true;
              console.log('user logged in');
          },
          loggedIn: function () {
              return status.isLoggedIn;
          },
          logout: function () {
              status.isLoggedIn = false;
              console.log('user logged out');
          }
      }
  });
  
  app.controller('AuthViewCtrl', function ($scope, AuthService) {
      // bind some service attributes, functions to the scope so that we can use them in our view
      $scope.loggedIn = AuthService.loggedIn;
      $scope.login = AuthService.login;
      $scope.logout = AuthService.logout;
      $scope.stat = AuthService.status;
  });
  
  // a simple directive to allow elements to log out of the app on click
  app.directive('appLogout', function (AuthService) {
      return function (scope, element) {
          element.bind('click', function () {
              AuthService.logout();
          });
      }
  });
  
  // a simple directive to allow elements to log into the app on click
  app.directive('appLogin', function (AuthService) {
      return function (scope, element) {
          element.bind('click', function () {
              AuthService.login();
          });
      }
  });
  </code></pre>
  
  <p>And the accompanying html:</p>
  
  <pre><code>&lt;div ng-app="app"&gt;
      &lt;div ng-controller="AuthViewCtrl"&gt;
          &lt;strong&gt;Are we logged in?&lt;/strong&gt;
          &lt;ul&gt;
              &lt;li&gt;service func on scope: &lt;strong&gt;{{ loggedIn() }}&lt;/strong&gt;&lt;/li&gt;
              &lt;li&gt;service prop on scope: &lt;strong&gt;{{ stat.isLoggedIn }}&lt;/strong&gt;&lt;/li&gt;
          &lt;/ul&gt;
  
          &lt;button ng-click="login()"&gt;log in from controller scope&lt;/button&gt;
          &lt;button ng-click="logout()"&gt;log out from controller scope&lt;/button&gt;
          &lt;button ng-click="loggedIn()"&gt;call AuthService.loggedIn()&lt;/button&gt;
      &lt;/div&gt;
  
      &lt;button app-login&gt;log in from directive&lt;/button&gt;
      &lt;button app-logout&gt;log out from directive&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The app is logged out when you start. If you "log in[/out] from controller," which calls a service function published to the scope, the watched service values are updated immediately in the view. However, if you hit "log in[/out] from directive," the watched service values are not updated (they will update if you simply call AuthService.loggedIn() from within the scope). </p>
  
  <p>So, I guess my question is, what's the best way to handle this? Where have I gone astray with watching service values? </p>
  
  <p>Thanks,
  Adam</p>