Work with multiple factories in angular

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

My plants are as follows:

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 insert them into 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 also load my factories:

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

And I enter the factories in 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 correct way to work with multiple factories?

+4
source share
2 answers

Check if the script is imported into the index file. You need files from both services that will be imported after the angular.js file.

+2

, controller, factories.

-

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

Source: https://habr.com/ru/post/1617280/


All Articles