1

I have two modules tms2 and tms_sub . I need to access mainCtrl inside mCtrl. Is this possible.

Tms

var tms2 = angular.module('tms2', ['tms_sub']);
tms2.controller("mCtrl", ["$scope","$controller", function ($scope,$controller) {
$scope.test = "a1";
$scope.testClick = function () {
   }
}]);

Tms_sub

var tms_sub = angular.module("tms_sub", []);
tms_sub.controller("mainCtrl", ["$scope", function ($scope) {
   $scope.test ="a"
   $scope.testClick1 = function() {
       alert($scope.test);
   }
}]);

How to call the function testClick1() form mainCtrl of tms_sub module inside function testClick(){} in mctrl of tms2 module.

$scope.testClick = function() {
       testClick1()
   }

}

bharath
  • 845
  • 2
  • 11
  • 23
  • 2
    See this question: http://stackoverflow.com/questions/25417162/how-do-i-inject-a-controller-into-another-controller-in-angularjs – ababashka Oct 15 '15 at 11:37
  • Btw follow cheekybastard's answer on the link above instead of the accepted answer. – Ronald91 Oct 15 '15 at 13:19

2 Answers2

1

Tip: Create separate service for common methods and inject into both controllers.

codeninja.sj
  • 3,452
  • 1
  • 20
  • 37
0

Did you tried this:

var tms2 = angular.module('tms2', ['tms_sub']);
tms2.controller("mCtrl", ["$scope","$controller”,"mainCtrl", function ($scope,$controller, mainCtrl) {
$scope.test = "a1";
$scope.testClick = function () {
   }
}]);
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108