1

I have an angular application with two controller. Here's a simplified version of the code:

var app = angular.module('walkerApp', ['firebase']);

app.controller('AuthenticationController', function($scope) {
    function login(user) {
      ...
    }
});

app.controller('StepsDataController', function($scope) {

});

The StepsDataController serves up data from a firebase backend. The AuthenticationController handles user management. When the current user changes (via the login or register methods of the AuthenticationController,) the StepsDataController should rebind to a set of data specific to that user.

How do I do this in Angular? Obviously, I need some sort of observer implementation, but I'm not sure what the mechanism is.

PSL
  • 123,204
  • 21
  • 253
  • 243
Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
  • Although I'm not sure, it seems to me that `StepsDataController` should be a service, not an app controller - is there a reason its a controller? – Izhaki Aug 27 '14 at 21:47
  • You could use eventing or you can implement a [pub/sub as in this example](http://stackoverflow.com/questions/25274563/angularjs-communication-between-directives/25274665#25274665). – PSL Aug 27 '14 at 21:51
  • I simplified out the service, which needs to know what portion of the data hive to look at depending on the currently logged-in user. – Yes - that Jake. Aug 27 '14 at 21:52
  • When an the user changes via Authentication controller, you need to refresh the other controller? In reality you have a service which `StepsDataController` uses to fetch the current user information and both the controllers are on the same page at a time? – PSL Aug 27 '14 at 22:01

1 Answers1

3

You can always use $scope.$broadcast or $scope.$emit from the subject controller, and $scope.$on on the observer controller. See docs for more.

I'm not sure how your controllers scopes are related hierarchy wise, but you can always user $rootScope.$broadcast.

Izhaki
  • 23,372
  • 9
  • 69
  • 107