Skip to content
Loading
  
  •   
  •   
  • var app = angular.module('MyApp', []);  
  •   
  • app.controller('Ctrl1', function($scope) {  
  •     
  •   $scope.func1 = function() {  
  •     alert('Ctrl1 and func1')  
  •   }  
  •     
  •     $scope.$on("MyFunction", function () {  
  •         alert('Ctrl1 MyFunction')  
  •     });  
  • });  
  •   
  • app.controller('Ctrl2', function($scope) {  
  •     
  •   $scope.func2 = function() {  
  •     alert('Ctrl2 and func2')  
  •     scope.$emit("MyFunction");  
  •   }  
  •   
  • }); 
  •  
    0
  • 0
    I could sort my issue. this way i could call one function in a controller from another controller.
     
    here is the code snippet
     
    1st approach
    1. "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">  
    2. "MyApp">  
    3.     "Ctrl1">  
    4.         "func1()">func1  
    5.     
      
  •     
      
  •     "Ctrl2">  
  •         "func2()">func2  
  •       
  •   
  •   
  • var app = angular.module('MyApp', []);  
  •   
  • app.controller('Ctrl1', function($scope,$rootScope) {  
  •   
  •   $scope.func1 = function() {  
  •     alert('Ctrl1 and func1')  
  •   }  
  •   
  •     $rootScope.$on("MyFunction", function () {  
  •         alert('Ctrl1 MyFunction')  
  •     });  
  • });  
  •   
  • app.controller('Ctrl2', function($scope) {  
  •   
  •   $scope.func2 = function() {  
  •     alert('Ctrl2 and func2')  
  •     $scope.$emit("MyFunction");  
  •   }  
  •   
  • }); 
  •  2nd approach
    1. app.controller('Ctrl1', function($scope) {  
    2.   
    3.   $scope.func1 = function() {  
    4.     alert('Ctrl1 and func1')  
    5.   }  
    6.   
    7.     $scope.$on("MyFunction", function () {  
    8.         alert('Ctrl1 MyFunction')  
    9.     });  
    10. });  
    11.   
    12. app.controller('Ctrl2', function($scope, $rootScope) {  
    13.   
    14.   $scope.func2 = function() {  
    15.     alert('Ctrl2 and func2')  
    16.     //$scope.$emit("MyFunction");  
    17.     //USE rootScope  
    18.     $rootScope.$broadcast("MyFunction");  
    19.   }  
    20.   
    21. }); 
     thanks
    0