Working with multiple factories in angular

I was working with only 1 factory in angular, but now that my project has grown too large, I want to split the file in separate factories.

My factories are like this:

angular.module('factories')
.factory('auth', ['$http', '$state', '$window',
    function($http, $state, $window) {
        var auth = {};

        ......

        return auth;

Userfactory:

angular.module('factories')
.factory('userFactory', ['$http', '$state', '$window',
  function($http, $state, $window) {
    var userFactory = {};
    return userFactory;

I inject them in my controllers:

angular.module('controllers')
.controller('UserCtrl', ['$scope', '$state', 'auth', 'userFactory', 'Facebook',
    function ($scope, $state, auth, userFactory, Facebook) {

However I get the following error:

Error: [$injector:unpr] http://errors.angularjs.org/1.4.7/$injector/unpr?p0=userFactoryProvider%20%3C-%20userFactory%20%3C-%20UserCtrl

I'm also bootstrapping my factories:

angular.module('factories', []);

And I inject factories into app.js:

var app = angular.module('eva', ['ui.router', 'ngMaterial', 'ngMessages',
  'controllers', 'factories', 'ngAnimate', '720kb.socialshare',
  'angular-loading-bar', 'angular-svg-round-progress', 'pascalprecht.translate',
  'facebook']);

What is the proper way to work with multiple factories?


Check if you imported the script into your index file. You need files from both of services to be imported after the angular.js file.


Since factories are in a separate module so you need to set dependency for controller module because it is using factories from factories module.

Do something like

angular.module('controllers', ['factories'])
.controller('UserCtrl', ['$scope', '$state', 'auth', 'userFactory', 'Facebook',
function ($scope, $state, auth, userFactory, Facebook) {
链接地址: http://www.djcxy.com/p/77664.html

上一篇: 如何将工厂注入Angular的另一家工厂

下一篇: 在角度上使用多个工厂