0

I have a login form directive with its own controller. The controller basically does the user login when the form is submittted fine. This works absolutely fine.

I am trying to contain my user login / logout functions in one controller in this case the LoginCtrl. I have a logout button outside the directive somewhere in the header part. Is it possible for me to call the doLogout() function within the LoginCtrl when that button is clicked ?

The only solution I have so far is to broadcast event on the rootScope and listen for the same on the LoginCtrl.

Any other alternatives ?

2 Answers 2

2

You could keep the actual functionality wrapped in a service (might want to do that anyway). This way you can use standard dependency injection to inject the service into whatever controller contains the logout button, and let it handle everything like a route change, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

ditto on this answer. services are perfect for this. I prefer singleton services to share state globally on the page vs broadcasting events. Although, nothing wrong with broadcasting either.
There is nothing wrong with broadcasting until you have multiple controllers/templates with logout buttons and then you're duplicating logic at that point.
0

The directive is not necessarily bind to a controller, in which case it can has its own scope. That is to say, you can pass the callback directly to the directive via the directive scope "&" operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.