AngularJs Factory in another file

0

Good morning

I want to create a separate file for each factory that I have to declare in my project, but it is not working:

App.js file

var GestaoGastos = angular.module('GestaoGastos', ['ngMaterial', 'ngRoute', 'ngMessages', 'ngResource']);

Post-service.js file

var CargoService = function(q, http) {

    var self = {}

    self.salvar = function(cargo) {
        var deferred = q.defer()

        http.post('/services/cargo/salvar', cargo).then(function(response) {
            deferred.resolve(respoonse)
        }, function(response) {
            deferred.reject(response)
        })

        return deferred.promise
    }

    return self
}

CargoService.$inject = ['$q', '$http']

angular.module('GestaoGastos').factory('CargoService', CargoService)

And the post-cadastro-controller.js file

var CargoCadastroController = function(
        scope, http, cargoService, RetornoChamadoStatus) {

    scope.cargo = {
        codigo: '',
        descricao: ''
    }

    scope.salvar = function() {

        cargoService.salvar(scope.cargo).then(function(response) {

            var retorno = response.data

            if (retorno.status == RetornoChamadoStatus.OK) {
                alert(retorno.mensagem)
            } else {
                alert(retorno.mensagem)
            }
        }, function(response) {

        })
    }

}

CargoCadastroController.$inject = ['$scope', '$http', 'CargoService', 'RetornoChamadoStatus']

GestaoGastos.controller('CargoCadastroController', CargoCadastroController)

But the following error appears:

Error: [$ injector: unpr] link $ injector / unpr? p0 = CargoServiceProvider% 20% 3C- % 20CargoService% 20% 3C-% 20CargoCadastroController

If we access the link we can see:

Error: $ injector: unpr Unknown Provider Unknown provider: CargoServiceProvider

asked by anonymous 08.01.2016 / 13:42

1 answer

2

First, in your files you are creating a new application every time.

Use the name of the variables with $ that the Inject is automatic, do not need to explicit it

Separate the file that makes the declaration files include:

For example file-business.js

var app = angular.module('GestaoGastos', ['ngMaterial', 'ngRoute', 'ngMessages', 'ngResource']);

app.factory('CargoService', CargoService);
app.controller('CargoCadastroController', CargoCadastroController);
    
08.01.2016 / 14:21