The problem is that a controller specified for a route template only exists
in memory at the time of the route match.  As soon as you leave and come
back to a route/view, no state is maintained on the controller, the view
runs the controller function all over again.  This is probably why you are
confused about ClientController not getting the $broadcast event... it must
be the active route/view to be listening.

You will have to store the user data in a service and inject it (or use
rootScope or MainCtrl scope since those don't get removed).   Hence, you
are rebuilding your scope each time you load the route: /client/:user_id/
account

function clientController($scope, AuthFactory, DataService){
  $scope.userData = DataService.userData; //by putting object data on
scope, any update to DataService.userData from other controller
automatically binds to the service and therefor the loaded view template
}


On Sun, Apr 20, 2014 at 9:49 PM, Billy Figueroa <[email protected]> wrote:

> I have not decided yet of how I am going to handle updating the user
> status button to login or logout. Not sure what controller I want to put it
> in yet so that is why you see multiple controllers with a "user.isLoggedIn"
> attribute, so just ignore that.
>
> As far as why not grab the data from the auth service...
>
> This is my Auth service which should only handle login/ logout etc The
> broadcasting is so I can put listeners on controllers who will then grab
> data from my "userData" or "dataFactory". This service will grab all the
> data I need from from the backend and then I can grab the data from both my
> "client" and "provider" controllers.
>
> something like this inside the client controller
>
> $scope.$on('clientAuthenticated', function(phpData) {
>     $scope.clientData = userData.getData()
> });
>
>
>
> On Sunday, April 20, 2014 11:02:00 PM UTC-4, Luke Kende wrote:
>
>> So, here's what I am understanding about your order of events, simplest
>> scenario...
>>
>> 1. User comes to home page and is not logged in therefore sees Login form.
>> 2. User enters credentials and clicks login, which let's say is
>> successful, so factory method sets it's local isLoggedIn value to true and
>> broadcasts the event.
>> 3.  At this point in time, the MainController and the LoginController are
>> still active controllers on the page.  MainController gets the broadcast
>> (but not sure why, maybe to change the state of the menu navigation?).
>>  LoginController runs the promise.then() function which sets it's own state
>> variable for isLoggedIn (but not sure why here either as the view is about
>> to change), then redirects the angular route so the new view is loaded with
>> the ClientController to show the account info.
>>
>> Why does the ClientController need the broadcast?  To get the phpData?
>>  If this is true, I'd say put the object data on the factory instance and
>> by injected it into the ClientController you will have access to the user's
>> logged in data (or anywhere throughout the app).
>>
>> factory.user = {
>>         isLoggedIn : false,
>>         data: null
>>     };
>>
>> // in $http promise.then()
>> if (phpData.account_type === 'client') {
>>     // $timeout(function() {
>>     broadcast('clientAuthenticated', phpData);
>>     factory.user.data = phpData;
>>    // }, 1000);
>>    return phpData;
>> }
>>
>>
>>
>>
>> On Sun, Apr 20, 2014 at 7:54 PM, Billy Figueroa <[email protected]>wrote:
>>
>>> FIY ignore the time functions. I was only using those to see if the
>>> broadcast worked. This is not part of my original code
>>>
>>>
>>> On Thursday, April 17, 2014 9:53:56 PM UTC-4, Billy Figueroa wrote:
>>>>
>>>> Hi All,
>>>>
>>>> so I have a quick issue with my controller ordering I guess.
>>>>
>>>> I have the following shell page layout...
>>>>
>>>> <html>
>>>>    <head></head>
>>>>    <body ng-controller="MainController">
>>>>       <div ng-view></div>
>>>>    </body>
>>>> </html>
>>>>
>>>> I have an ng-view template that the routeProvider is loading
>>>> (account.php) when we hit the url and load ClientController...
>>>>
>>>>     .when('/client/:user_id/account', {
>>>>         templateUrl: '../MINT/views/account.php',
>>>>         controller: 'ClientController',
>>>>         restrict: true
>>>>     })
>>>>
>>>> I also have an AuthFacotory factory where, once a user logs in and I
>>>> get data back from my backend (PHP) I broadcast a signal to say we are
>>>> authenticated
>>>>
>>>>                     if (phpData.account_type === 'client')
>>>>                         $rootScope.$broadcast('clientAuthenticated',
>>>> phpData);
>>>>                     else
>>>>                         $rootScope.$broadcast('providerAuthenticated',
>>>> phpData);
>>>>
>>>> I have the following I guess "watches" both inside the MainController
>>>> and ClientController
>>>>
>>>>
>>>> <-- MainController -->
>>>>     $scope.$on('clientAuthenticated', function(phpData) {
>>>>         console.log("We caught the broadcast for clientAuthenticated[
>>>> MainController]");
>>>>     });
>>>>
>>>> <-- ClientController -->
>>>>     $scope.$on('clientAuthenticated', function(phpData) {
>>>>         console.log("We caught the broadcast for clientAuthenticated[
>>>> ClientController]");
>>>>     });
>>>>
>>>>
>>>> I am only "catching" the one in the MainController.
>>>>
>>>> Based on the structure being that the ClientController is loaded as
>>>> part of the view, I m guessing its some sort of a child of the
>>>> MainController but why is it not "catching" that broadcast?
>>>>
>>>> the weird part is that I have other code in the ClientController and
>>>> that gets executed but the console.log inside the scope statement is not
>>>>
>>>> I wanted to create a jsfiddle or plunker but I rarely get them working
>>>> when I try to do it in a non global modular way like real apps are written
>>>> i.e. var myApp = angular.module('myApp', [])
>>>>
>>>  --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "AngularJS" group.
>>> To unsubscribe from this topic, visit https://groups.google.com/d/
>>> topic/angular/NaBscLyPqhY/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> Visit this group at http://groups.google.com/group/angular.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to a topic in the
> Google Groups "AngularJS" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/angular/NaBscLyPqhY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to