How to separate my factory in another .js file?

0

my question is this, I'm learning AngularJS, and I'm doing the code for a login, where I've done factory's, but everything is in the same file (app.js) as shown below:

'(function () {     var app = angular.module ('login', ['ngStorage']);

//factoria para guardar y eliminar sesiones con sessionStorage
app.factory('sesionesControl', function($sessionStorage){
    return {
        //obtenemos una sesión //getter
        get : function() {
            return $sessionStorage.LoginAIRSoftware;
        },
        //creamos una sesión //setter
        set : function(val) {
            return $sessionStorage.LoginAIRSoftware = val;
        },
        //limpiamos una sesión
        unset : function() {
            return $sessionStorage.LoginAIRSoftware = undefined;
        }
    };
});



//Controlador para loginForm
app.controller('loginForm', ['$scope', '$window', '$location', 'authUsers', 'sesionesControl', 
    function($scope, $window, $location, authUsers, sesionesControl){

    $scope.inicio = function(){
        if($scope.usuario === null || $scope.password === null){
            $scope.mensajeError = "Uno o más campos se encuentran vacios";
            $scope.mensajeShow = true;
        }else{
            authUsers.login($scope.usuario, $scope.password)
            .success(function(res) {
                if(res.data === 'Correcto'){
                    sesionesControl.set($scope.usuario);
                    $window.location.href = 'home.html';
                }else {
                    $scope.mensajeError = "USUARIO y/o CONTRASEÑA invalidos. Intente nuevamente.";
                    $scope.mensajeShow = true;
                    return false;
                }
            })
            .error(function(err) {
                $scope.mensajeError = "Error al conectar con Servidor";
                $scope.mensajeShow = true;
            });
        }
    };

}]);
})();'

I would like to know how to separate the factory "sessionsControl" in another file called factorySesionesControl.js and that my app.js file can use it. I would put what I tried, but it is useless since it does not work, and for that I would appreciate your help.

    
asked by Angel Perez 05.05.2017 в 16:13
source

2 answers

0

Well, you just have to put the factory code in that separate file and include it after the app in the index.html

That is, index.html:

<head>
        <script src="angular.js"></script>
        <script src="app.js"></script>
        <script src="factorySesionesControl.js"></script>
</head>
    
answered by 05.05.2017 в 16:30
0

It could be like this, you create your file separately factory.js and inside

angular.module('login').factory('sesionesControl', function($sessionStorage){
    return {
        //obtenemos una sesión //getter
        get : function() {
            return $sessionStorage.LoginAIRSoftware;
        },
        //creamos una sesión //setter
        set : function(val) {
            return $sessionStorage.LoginAIRSoftware = val;
        },
        //limpiamos una sesión
        unset : function() {
            return $sessionStorage.LoginAIRSoftware = undefined;
        }
    };
});

You could do the same with the drivers, directives, etc. Simply instead of using a variable in an "app" file, you replace it with "angular.module ('login'). Controller" for example, etc., luck!

    
answered by 06.05.2017 в 04:44