0

I have two controllers and i have to call one controller from another controller.When i called the controller directly i got Error: "$injector:unpr unknown provider".I don't want to create a service or event handling.Is there any simpler way that i can call it?

Below are the controllers:

app.controller('firstCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});

app.controller('secondCtrl',function($scope){
$scope.occupation = "Student";
});

When i called secondCtrl from firstCtrl as below:

***app.controller('firstCtrl', function($scope,secondCtrl) //unknown provider.***
GrailsLearner
  • 485
  • 1
  • 11
  • 23
  • Why don't you want to create a service? This is the way it should be done if you want to share data between controllers.. Maybe you could use `$rootScope`, but that would be like using global variables in plain Javascript – Kaddath Jan 14 '19 at 09:19
  • Possible duplicate https://stackoverflow.com/questions/9293423/can-one-angularjs-controller-call-another – Nazır Dogan Jan 14 '19 at 09:24
  • 1
    Possible duplicate of [Can one AngularJS controller call another?](https://stackoverflow.com/questions/9293423/can-one-angularjs-controller-call-another) – Nazır Dogan Jan 14 '19 at 09:25
  • @Kaddath The above mentioned example is a simple one but in the real time i am already having the code written in a controller file.But in order to access that controller i don't have any service in common.So i am trying to figure out how to call it without moving the code to a service? – GrailsLearner Jan 14 '19 at 09:29
  • Then, you should adapt your question so that we can see what you are really trying to achieve. By the way, creating a service is not complicated, and if the logic should be shared across the app, this is the best solution – Kaddath Jan 14 '19 at 09:40
  • @Kaddath - This is our situation: 1. we have a directive which loads the datagrid for us and handles all the pagination and other functionalitites 2. this directive uses a controller for the crud activities 3. controller uses a service which supplies it with headers and colDef and rowdata. Now i have to call this controller from another controller. – GrailsLearner Jan 14 '19 at 09:48

0 Answers0