calling a controller function from another controller function

32 views
Skip to first unread message

maxim couton

unread,
May 5, 2014, 5:35:49 AM5/5/14
to ang...@googlegroups.com
Hi ,
   
    Suppose I have two controllers : 

   controller 1 : {
     
     $scope.operation1(){....}
     $scope.operation2(){....};

   } 

     controller 2 : {
     
      $scope.operation3(){....}
      $scope.operation4(){....};

   } 

  What I want to do is   for example :
 
    operation3(){
         controller1.operation1();    // invok operation1 which is defined in controller1 from operation3
    }  

   So my question is then is it possible to call a controller function from within another controller without using scope ?  
   On sollution is to do :
   
   operation3(){
      scope.operation1();
   }
  But I don't like it this way , I want to identiy a fuction using the controller where is is defined (like is java doing : controller1.operation1())


  Any Idea ?

 Thanks
   

Sander Elias

unread,
May 5, 2014, 5:38:22 AM5/5/14
to ang...@googlegroups.com
Hi Maxim,

This is a textbook example of the use of the "controller as" syntax. 
If you would have provided an plunker, I would have shown you how to do it.

Regards
Sander

maxim couton

unread,
May 5, 2014, 5:54:01 AM5/5/14
to ang...@googlegroups.com
Hi Sander,
    I wants somethig like this;

   angular.module('mymodule').controller('C1' , function ($scope)){


    $scope.operation1(params){
       
     // do somthing

    }

});



angular.module('mymodule').controller('C2' , function ($scope)){


    $scope.call(params){
       
     c1.operation1(params); // ??? is it possible and how to make C2 knows C1 

    }

}); 

Tony pee

unread,
May 5, 2014, 8:34:11 AM5/5/14
to ang...@googlegroups.com
controllers should not know about eachother, so they can share functionality via a service, or via events


--
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 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.



--
Tony Polinelli

Message has been deleted

maxim couton

unread,
May 5, 2014, 9:08:26 AM5/5/14
to ang...@googlegroups.com
Hi, 

  Thank you for respondig, 

    In fact I know that services can be used to share functionality,  but I want to implement proxy pattern 
so controller would have a possibility to communicate with another.

------
Maxim

Maruf Siddiqui

unread,
May 5, 2014, 9:19:02 AM5/5/14
to ang...@googlegroups.com
Not sure if it can help. https://docs.google.com/file/d/0B4F6Csor-S1cNThqekp4NUZCSmc/edit page 30, and the corresponding example http://jsfiddle.net/8M27W/8/
Maruf Siddiqui
Senior Software Engineer
Newscred Inc.


maxim couton

unread,
May 5, 2014, 9:46:23 AM5/5/14
to ang...@googlegroups.com
Thank you I will see it

maxim couton

unread,
May 5, 2014, 9:55:53 AM5/5/14
to ang...@googlegroups.com
Interesting presentation. At some point they talked about controller inheritance but that was 't what I am searching for. 

regards,
Maxim

Le lundi 5 mai 2014 15:19:02 UTC+2, Maruf Siddiqui a écrit :

Sander Elias

unread,
May 5, 2014, 10:50:19 AM5/5/14
to ang...@googlegroups.com
Hi Maxim,

As long as you travel down in the scope hierarchy, this is following angular conventions. 

If you need to call controllers from the bottom up, or from sibling controllers, you probably have a code smell. 
If you need something like that, you need or a service, or events. (or a callback system, but thats harder to realize)

Is this what you need/want? If not, adapt the plunker to what you intend to, and ask again!

Regards
Sander
 

maxim couton

unread,
May 5, 2014, 11:39:05 AM5/5/14
to ang...@googlegroups.com
Hi,  
 Thanks,
  
  That is what I am searching for.  

Best Regards


--
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/o6Hqqjk5KJo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages