AngularJS: Inject controller inside another controller from the same module

Joseph Freeman picture Joseph Freeman · Apr 28, 2015 · Viewed 30.8k times · Source

Is possible to inject a controller into another controller that is part of the same module?

example:

I keep getting unknown provider on controllerOne. I don't see how that's possible since they coexist in the same module. Any help would be much appreciated.

Answer

Pankaj Parkar picture Pankaj Parkar · Apr 28, 2015

You need to use $controller dependency by using that you can inject one controller to another

.controller('controllerTwo', ['$scope', '$controller', function($scope, $controller){
  $controller('controllerOne', {$scope: $scope})
  //inside scope you the controllerOne scope will available
}]);

But do prefer service/factory to share data