1

I have two ng-controllers for example controller1, controller2, i want to inject controller2 method in controller1 and i want to access through my JS API function using "document.querySelector('[ng-controller=custom-entity-design-ctrl]')". Is it possible to get? I tried using factory services that also not working. It's saying error $scope not defined.

source code

**controller1**
  myApp.controller("controller1",function($scope, $document, $http, $localStorage) {
        $scope.test1 = function() {
              alert("test1");
         };
  });

  **controller2**
  myApp.controller("controller2",function($scope,$http,$compile,$localStorage, $log) {
        $scope.test2 = function() {
              alert("test2");
         };
  });

In detail... i want to access $scope.test2 method from controller1.

I tried using factory also not working.

source code:

  myApp.factory("testService", function($compile, $http, $scope) {     
   $scope.test2 = function() {
              alert("test2");
         };   
   }

   factory injected in controller1

   myApp.controller("controller1",['testService',function($scope, $document, $http, $localStorage) {
        $scope.test1 = function() {
              alert("test1");
         };
  }]);

Can anyone help me to achieve this.

4
  • 3
    you cant inject a controller in to another, please have a look at services. Commented Sep 30, 2015 at 7:20
  • Use a factory or service Commented Sep 30, 2015 at 7:37
  • @pixelbits . i tried using factory it is not working. please have a look my fiddle jsfiddle.net/bagya1985/4o7rcj4y Commented Sep 30, 2015 at 7:45
  • 1
    You must use Service, not Factory. Service, we use where we need to share data among controllers. Factory, we use where we need to create different-2 instance of an object. Commented Sep 30, 2015 at 8:01

1 Answer 1

1

You were at the right path with use of .factory():

myApp.factory("testService", function($compile, $http, $scope) {     
   // just return the things in an object.
   return {
      name:"angular js",
      test2 : function(){ alert("test2") }
   }  
});

myApp.controller("controller1",['$scope', '$document', '$http', '$localStorage','testService', function($scope, $document, $http, $localStorage, testService) {
    $scope.test1 = function() {
        alert("test1");
        testService.test2(); // use the method from the testService.
    };
}]);

You were missing the service in the function args of the controller and when you use .factory() service then make sure to return everything in an object {}, with this object you can provide multiple values/methods etc.

Check the plnkr.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.