Thursday 15 May 2014

How to reuse angularjs initialised variables in other controllers -


I have the angular rail code .run () , so when the app is initialized, So i set the root under 2 wars. Because I decide to use these wires in every way (pages) and controllers. async

  app.run (function ($ Rootscope, $ timeout, firebase) {$ Rootscope. $ ('FirebaseLoggedIn', function (event, authUser) {$ Timeout (function () {$ rootScope._isLoggedIn = true; $ rootScope._authUser = authUser;}, 0);});   

But here's the problem, when I run it in my Controllers, it always shows undefined. Unless I do not put it in the function block, so my guess is that the controller is run before the value of .run () vars ;

  App.controller ('foo', function ($ scope) {console.log ($ scope._authUser); // always unspecified // unless I do the following ///scope.clickme = function () {Console.log} ($ Scope._authUser)};});   

Any suggestions I'm able to use async from .s (.in)?

If I call the following code in every controller, it seems that I keep repeating myself.

  $ rootScope. $ ('FirebaseloggedIn', function (event, authUser } {});   

UPDATE is here where the Dean event from firebaselog I think that will re-mention me again. The callback is async.

  App Service ('firebase', ['$ rootscope', function ($ Rootscope)) {// Defined Firebase DB URL this.firebaseRef = new Firebase ("https: //test.firebaseio.com"); This.onLoginStateChanges = new FirebaseAuthClient (this.firebaseRef, function (error, user) {if (user) {$ rootScope. $ Emit ("firebaseLoggedIn", user); // certified user} else if (error) {$ rootScope. $ Emit ("firebaseloginError", error); // authentication failed} and {$ Rootscope. $ Emit ("firebaseLogout"); // user login}}}}});    

The injector is loading all the modules for your app. So your assumption about your controller starts for the first time.

What you're doing effectively with the $ timeout (function () {...}, 0), forcing the code to run the next code is not the digest loop but me It seems that this is what you are trying to accomplish.

I am not sure where 'firebase log' is being broadcasted; But you may find that this complete implementation will be more straightforward as an angular service.

  app ('FireBaseService', function ($ Rootscope)) has been recovered through the methods given below. Var isLoggedIn = false; Var authUser = {}; // This is the only place that you handle logged in events, set user references. $ RootScope $ ('FirebaseLoggedIn', function (event, authUser) {IsLoggedIn = true; authUser = authUser;}); Return some functions to expose the above variables {getIsLoggedIn: function () {return isLoggedIn;}, getAuthUser: function () {return authUser;}}});   

then in your controller ......

  app.controller ('foo', function ($ radius, $ log, firebase service) { If (FireBaseService.getIsLoggedIn ()) {$ log.info (FireBaseService.getAuthUser (.name);}}};   

This enables you to access these variables wherever you are injecting FireBaseService service.

No comments:

Post a Comment