Notify other functions in AngularJS

48 views
Skip to first unread message

Masoud gheisari

unread,
Jun 18, 2014, 10:08:53 AM6/18/14
to ang...@googlegroups.com
Hi everyone,

I have checked some of the topics for this matter and i got an understanding of controllers are there to initiate scope and i need to use services for this matter but i dont know how.

so here is the problem. i have index page which body has only one div and inside the div i have ng-include listening to a function called viewFile() which is described on controllerA. on the first initial attempt i load a view called login.html and display it. when users logs in and its successful, which are handled in controllerB, i return a token and now i want to load main.html page using viewFile() in controllerA. is there a call back function or notify controller or something for this? or can i write a service that takes care of this for me?

I'm not using ngRoute because i dont want my URL to change to mysite.com/#/login.html and then mysite.com/#/main.html

Gordon Bockus

unread,
Jun 19, 2014, 1:53:56 AM6/19/14
to ang...@googlegroups.com
There are a lot of unknowns here, but yes a service seems like the right answer.

Basically you'll inject the service into both of your controllers and then update a value in the service from controllerB.  Then in controller a you should have a watch on that service value and update according when it changes.


Simple service implementation 
angular.module('services.auth', [])
  .service('AuthService', function() {

    return {
      loggedIn: false
    };

  });

Although I would also suggest moving what ever you are doing in controller be that is interacting with the server to the service as well.  Keep $http out of your controllers.

Gordon

Masoud gheisari

unread,
Jun 19, 2014, 11:18:43 AM6/19/14
to ang...@googlegroups.com
Very good respond, i have taken out all of that and created a service to do Authentication. now my question still remains. here is part of my code:

<div ng-controller="defaultCtrl">
        <ng-include src="viewFile()"></ng-include>
    </div>

angular.module('app', [])
  .controller('mainCtrl', function() {

    $scope.viewFile = function(){
       if( condition A)
             retrun viewA;
       else
           return viewB;
    }

  });

.service('AuthService', function() {
   $http({
       get ....
        .success: function (data){
          // INVOKE viewFile() FROM CONTROLLER ABOVE?????
        }
    });
});


i want to know if there is a way that i can define viewFile in controller and then anfter using a service, invoke the function to do the updates. if not, would i be able to create viewFile() function in a service and still bind it to ng-inclue in my html file?




Gordon Bockus

unread,
Jun 19, 2014, 11:59:49 AM6/19/14
to ang...@googlegroups.com
Hi Masoud, 
No need to invoke viewFile manually.  The angular digest loop will take care of it for you.  You just need to set your conditions in the controller to switch template files based off some value found in the service. 

Check out this plunk

Gordon


--
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/ZfPe9-NFmLU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages