Skip to main content

AngularJs call one method of controller in another controller .

I have seen many question about calling one method of one controller in another controller or extending scope of one controller in another controller.so here are the ways.

if you want to call one controller into another or extending scope of controllers there are four methods available
  1. $rootScope.$emit() and $rootScope.$broadcast()
  2. If Second controller is child ,you can use Parent child communication .
  3. Use Services
  4. Kind of hack - with the help of angular.element()
1. $rootScope.$emit() and $rootScope.$broadcast()
Controller and its scope can get destroyed, but the $rootScope remains across the application, that's why we are taking $rootScope because $rootScope is parent of all scopes .
If you are performing communication from parent to child and even child wants to communicate with its siblings, you can use $broadcast
If you are performing communication from child to parent ,no siblings invovled then you can use $rootScope.$emit
HTML
<body ng-app="myApp">
    <div ng-controller="ParentCtrl" class="ng-scope">
      // ParentCtrl
      <div ng-controller="Sibling1" class="ng-scope">
        // Sibling first controller
      </div>
      <div ng-controller="Sibling2" class="ng-scope">
        // Sibling Second controller
        <div ng-controller="Child" class="ng-scope">
          // Child controller
        </div>
      </div>
    </div>
</body>
Angularjs Code
 var app =  angular.module('myApp',[]);//We will use it throughout the example 
    app.controller('Child', function($rootScope) {
      $rootScope.$emit('childEmit', 'Child calling parent');
      $rootScope.$broadcast('siblingAndParent');
    });

app.controller('Sibling1', function($rootScope) {
  $rootScope.$on('childEmit', function(event, data) {
    console.log(data + ' Inside Sibling one');
  });
  $rootScope.$on('siblingAndParent', function(event, data) {
    console.log('broadcast from child in parent');
  });
});

app.controller('Sibling2', function($rootScope) {
  $rootScope.$on('childEmit', function(event, data) {
    console.log(data + ' Inside Sibling two');
  });
  $rootScope.$on('siblingAndParent', function(event, data) {
    console.log('broadcast from child in parent');
  });
});

app.controller('ParentCtrl', function($rootScope) {
  $rootScope.$on('childEmit', function(event, data) {
    console.log(data + ' Inside parent controller');
  });
  $rootScope.$on('siblingAndParent', function(event, data) {
    console.log('broadcast from child in parent');
  });
});
In above code console of $emit 'childEmit' will not call inside child siblings and It will call inside only parent, where $broadcast get called inside siblings and parent as well.This is the place where performance come into a action.$emit is preferrable, if you are using child to parent communication because it skips some dirty checks.
2. If Second controller is child, you can use Child Parent communication
Its one of the best method, If you want to do child parent communication where child wants to communicate with immediate parent then it would not need any kind $broadcast or $emit but if you want to do communication from parent to child then you have to use either service or $broadcast
For example HTML:-
<div ng-controller="ParentCtrl">
 <div ng-controller="ChildCtrl">
 </div>
</div>
Angularjs
 app.controller('ParentCtrl', function($scope) {
   $scope.value='Its parent';
      });
  app.controller('ChildCtrl', function($scope) {
   console.log($scope.value);
  });
Whenever you are using child to parent communication, Angularjs will search for a variable inside child, If it is not present inside then it will choose to see the values inside parent controller.
3.Use Services
AngularJS supports the concepts of "Seperation of Concerns" using services architecture. Services are javascript functions and are responsible to do a specific tasks only.This makes them anindividual entity which is maintainable and testable.Services used to inject using Dependency Injection mecahnism of Angularjs.
Angularjs code:
app.service('communicate',function(){
  this.communicateValue='Hello';
});

app.controller('ParentCtrl',function(communicate){//Dependency Injection
  console.log(communicate.communicateValue+" Parent World");
});

app.controller('ChildCtrl',function(communicate){//Dependency Injection
  console.log(communicate.communicateValue+" Child World");
});
It will give output Hello Child World and Hello Parent World . According to Angular docs of servicesSingletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.
4.Kind of hack - with the help of angular.element()
This method gets scope() from the element by its Id / unique class.angular.element() method returns element and scope() gives $scope variable of another variable using $scope variable of one controller inside another is not a good practice.
HTML:-
<div id='parent' ng-controller='ParentCtrl'>{{varParent}}
 <span ng-click='getValueFromChild()'>Click to get ValueFormChild</span>
 <div id='child' ng-controller='childCtrl'>{{varChild}}
   <span ng-click='getValueFromParent()'>Click to get ValueFormParent </span>
 </div>
</div>
Angularjs:-
app.controller('ParentCtrl',function($scope){
 $scope.varParent="Hello Parent";
  $scope.getValueFromChild=function(){
  var childScope=angular.element('#child').scope();
  console.log(childScope.varChild);
  }
});

app.controller('CarentCtrl',function($scope){
 $scope.varChild="Hello Child";
  $scope.getValueFromParent=function(){
  var parentScope=angular.element('#parent').scope();
  console.log(parentScope.varParent);
  }
}); 
In above code controllers are showing their own value on Html and when you will click on text you will get values in console accordingly.If you click on parent controllers span, browser will console value of child and viceversa.

Comments

Post a Comment

Popular posts from this blog

OX Alpha: Nobody Knew Who Made It, and Teams Still Put It in Production

A model called OX Alpha showed up on OpenRouter on August 20. No lab name. No announcement. No documentation beyond a spec sheet: 1,048,576-token context, text and image and video input, function calling, free to use until August 27. Within 24 hours, people were routing production traffic through it. The benchmark claim driving that adoption: 80% on DeepSWE Pass@1. For context, the numbers being passed around put Claude Fable at 65% and GPT-5.6 Sol at 52%. Those numbers came from a ten-task community test, not an audited leaderboard, but that didn't slow anyone down. I'm not here to litigate whether OX Alpha is actually better at coding. I'm more interested in what happened to people's due diligence. What "Stealth" Actually Means on OpenRouter OpenRouter runs a program where labs can preview models anonymously. The provider shows up as "Stealth," and the model name is whatever the lab chooses. The idea is straightforward: labs get real-world ev...

Muse Glimmer: Apache 2.0, 30B, and Why the EU AI Act Just Made Your Model License a First-Class Decision

Meta shipped Muse Glimmer on August 10, and the thing that matters most isn't the benchmark scores. It's the license. After more than a year of closed models, Meta put a 30B parameter agentic model on Hugging Face under Apache 2.0 with no strings attached. That decision landed two weeks after the EU AI Office gained actual enforcement teeth over general-purpose AI model providers. The timing isn't coincidental. What You're Actually Getting Muse Glimmer is a 30B model distilled from Muse Spark. The distillation kept the agentic capabilities, the multi-step reasoning, and the function-calling quality while cutting the parameter count to something you can deploy locally without a data center. At full precision, 30B needs north of 55 GB of memory. In 4-bit K-Quant, the footprint drops to 17-20 GB. A single RTX 4090 or RTX 3090 (both 24 GB) handles it with room left for the KV cache. On a Mac with an M4 Max or M5 Max you have even more headroom. Meta built DFlash specul...