Calling a function in another controller

5,578 views
Skip to first unread message

Brad McAlister

unread,
Jan 11, 2013, 1:57:37 PM1/11/13
to ang...@googlegroups.com
How do I call a function in ControllerB from a call or an event in ControllerA? So far the answers I've seen for communicating between controller involve services and that's not what I'm trying to do here. I just need to access a function that may not reside in the controller it's being accessed from. Is that possible?



Joshua Miller

unread,
Jan 11, 2013, 1:59:57 PM1/11/13
to angular
Hello!

This really _is_ a job for services. What do you mean by "that's not what I'm trying to do here"?

Josh


On Fri, Jan 11, 2013 at 10:57 AM, Brad McAlister <brad.mc...@gmail.com> wrote:
How do I call a function in ControllerB from a call or an event in ControllerA? So far the answers I've seen for communicating between controller involve services and that's not what I'm trying to do here. I just need to access a function that may not reside in the controller it's being accessed from. Is that possible?



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Pawel Kozlowski

unread,
Jan 11, 2013, 2:00:42 PM1/11/13
to ang...@googlegroups.com
Hi!

On Fri, Jan 11, 2013 at 7:57 PM, Brad McAlister
<brad.mc...@gmail.com> wrote:
> I just need to access a function that may not reside in the controller it's
> being accessed from

Are you really talking about functions defined on a controller? Or
functions that are exposed on a scope by a controller?

Cheers,
Pawel


--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Brad McAlister

unread,
Jan 11, 2013, 2:16:09 PM1/11/13
to ang...@googlegroups.com
I guess I don't know what I'm talking about really.

I *thought* I should have a controller for the code that handles the Notes in my app. But sometimes I want to bring those notes up from a button that might be in another controller. For a non-plunker example, I have a table and each row has an "edit" button that opens a modal. In that modal I display notes and allow adding a new note. I also have a select box that does a completely different function like assigning that row item being edited to a new category. That doesn't really have anything to do with Notes. So to me it seemed like I should separate the Notes stuff from the the new category stuff, from the main table stuff.

Am I thinking about this all wrong?

Peter Bacon Darwin

unread,
Jan 11, 2013, 3:37:18 PM1/11/13
to ang...@googlegroups.com
Controllers may not be what you think they are!
Forget the "control" connotation that you might have from server side MVC.
AngularJS application controllers are really just "code-behind", a way of initializing scope with data and functions.  No "controlling" going on there.
Pete


--

Brad McAlister

unread,
Jan 11, 2013, 3:41:21 PM1/11/13
to ang...@googlegroups.com
So putting everything for a "page" in one controller is good or bad? I had it all in one while trying to get things working but thought I should start trying to break it out. I guess I don't need to then? It's all just about scope not really DRY?

Pawel Kozlowski

unread,
Jan 11, 2013, 3:46:12 PM1/11/13
to ang...@googlegroups.com
Hi!

On Fri, Jan 11, 2013 at 9:41 PM, Brad McAlister
<brad.mc...@gmail.com> wrote:
> It's all just about scope not really DRY?

DRY holds true, no matter what :-)

But yes, as Peter have said, AngularJS controllers only initialize
things on a scope, namelly:
- set up initial data on a scope
- register functions to be accessible from a template
- register event handlers ($scope.$on)

This is it, no more. In particular, no routing / dispatching should be
managed in a controller.

Then, when it comes to a "page" - in AngularJS a page visible in a
browser is composed of many partials. Some of them can be rather small
(for example, a row in ng-repeat) and it is not uncommon to have
separate controllers for those small partials.

Looking at your code example to make this discussion less "abstract".

Brad McAlister

unread,
Jan 11, 2013, 5:25:58 PM1/11/13
to ang...@googlegroups.com
Hi Pawel,

I pretty much understand what you're saying until I get the this part:

 in AngularJS a page visible in a 
browser is composed of many partials. Some of them can be rather small 
(for example, a row in ng-repeat) and it is not uncommon to have 
separate controllers for those small partials. 

I think that's what I'm trying to accomplish with the Modal.

Here's a more complete and complicated example:


So the way I understand it now is that if I have a partial for a modal I can/should have a controller for that modal but the event to open the modal is in the main controller then anything that event will do needs to reside in that main controller even if the end result data will be displayed in the modal which is controlled by the modal controller. Do I have that right?

Thanks for the help.

ranadheer reddy

unread,
Nov 5, 2013, 7:32:49 AM11/5/13
to ang...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages