简体   繁体   中英

Observing changes across controllers in angular

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM