0

I have a controller like this :

  .controller('candidatureStartCtrl', function ($scope, $parse, fileUploadCtrl) {

//...code
      fileUploadCtrl.uploadFile($scope.identityDocUpload);
//...code
}

in candidatureStartCtrl I'm calling another controller which is fileUploadCtrl to use its function uploadFile().

but I get this error in console even though I imported the fileUploadCtrl js in the index.html :

 Error: [$injector:unpr] Unknown provider: fileUploadCtrlProvider <- fileUploadCtrl <- CandidatureStartCtrl

this is a fiddle with the same concept :

http://jsfiddle.net/7tzXh/143/

How can I solve this ?

7
  • According to the error your fileUploadCtrl is undefined. Make sure you've added proper reference Commented Apr 20, 2016 at 10:25
  • @Shreyas as I said I've imported the fileUploadCtrl js in the index.html Commented Apr 20, 2016 at 10:26
  • is it in correct order? Commented Apr 20, 2016 at 10:29
  • @Shreyas yes it is, I think its due to injection or something like this since I need to use $inject in that case but I dont know how, I'm still learning angularjs Commented Apr 20, 2016 at 10:34
  • 1
    Injecting controller in another controller. Injecting controller in another controller is not the best way to call a function. Maybe create a service or use some other mechanics like $scope.$broadcast(). Commented Apr 20, 2016 at 10:45

2 Answers 2

1

You cannot inject controllers directly without using $controller as mentioned in one of the comments to the question. If you need to share data, you can use factory or service.

Please find the working fiddle here:

http://jsfiddle.net/7tzXh/144/

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

1 Comment

Thanks I actually used that solution before and I wanted to ask if I can to use a controller inside another but since controllers get garbage collected on route changes using a service/factory is the best idea.
0

$controller is used to inject a controller into another. I've created a simple fiddle to make it clear.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.