0

Similar to this question: How to create separate AngularJS controller files?

I have a module that has controllers in several files, including controllers that are linked to directives. Where can I declare a factory such that it can be injected into all of them easily?

For example, in app.js I define the app level module var app = angular.module('myApp', ['ngRoute']);

I would then expect to be able to define a factory in app.js like app.factory('myFactory', function() { return { myThing: myThing}; });

And then be able to inject it into a controller that looked like

angular.module('myApp')
.controller('Ctrl1', ['$scope', '$http', 'myFactory', 
    function ($scope, $http, myFactory) {}]);

But I get the error Error: [$injector:unpr] Unknown provider:

Is there a way to do this?

Community
  • 1
  • 1
FutureShocked
  • 779
  • 1
  • 10
  • 26
  • Please, provide http://stackoverflow.com/help/mcve . Currently you have syntax error `app.factory('myFactory, function() {})`. It looks like you redefine myApp module with angular.module('myApp', []). Of course, it won't have myFactory, and it won't have ngRoute module loaded, too. – Estus Flask May 03 '17 at 21:56
  • Edited for corrections and clarity. – FutureShocked May 03 '17 at 22:16
  • Then I would say that getting `$injector:unpr` is not possible, at least for myFactory. The code above won't have problems with injecting myFactory to a controller. – Estus Flask May 03 '17 at 22:31

0 Answers0