1

I have a file with the name app.js and there, I use many controllers for my web applications. My problem is I repeat many times the same code. For example this $http POST

 $http({
        method: 'POST',
        url: urlProfile,
        data: $.param($scope.ProfileForm),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Token': '\UsernameToken Token="' + mycookie + '"',
        }
    }).success(function(data, status, headers, config) {
        $scope.showProfiles();
        $('#newProfile').modal('hide');
        $scope.notifications(
            $scope.title = 'Notificación',
            $scope.body = 'Se ha creado un nuevo perfil',
            $scope.icon = '/img/restore.png'
        );
    }).error(function(data, status, headers, config) {
        alert('No se ha guardado el Perfil con el nombre: ' + profilename);
    });

How can I use this method like Service o Factory in my controllers and only provide the params like URL or Method

3
  • 1
    it sounds like you answered your own question.. Commented Jun 23, 2015 at 22:27
  • Like this docs.angularjs.org/guide/services Commented Jun 23, 2015 at 22:30
  • the thing i would try is no write all that code every time i need, but I dont know how to use Services o Factories, I just read those are the best options Commented Jun 23, 2015 at 22:37

2 Answers 2

1

Create your own factory and return the promise object:

(function(){
    angular.module('test', []);

    angular.module('test')
        .controller('Controller1', ['$scope', 'myService', function($scope, myService){
            myService.myRequest(URL, $.param($scope.ProfileForm)).then(function(result){
                //result of ajax here, do your scope manipulation here
            });
        }])
        .controller('Controller2', ['$scope', 'myService', function(myService){
            myService.myRequest(URL, $.param($scope.ProfileForm)).then(function(result){
                //result of ajax here, do your scope manipulation here
            });
        }])
        .factory('myService', ['$http', function($http){
            return {
                myRequest: function(urlProfile, data){
                    return $http({
                        method: 'POST',
                        url: urlProfile,
                        data: data,
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'X-Token': '\UsernameToken Token="' + mycookie + '"',
                        }
                    }).then(function(result){
                        return result.data;
                    });
                }
            };
        }]);
})();
Sign up to request clarification or add additional context in comments.

3 Comments

Yeap, but he would also have to get rid of all the $scope variables in there before using this.
Sorry, was in the process of updating while you commented!
You missed one :P data: $.param($scope.ProfileForm),
0

I recommend following JohnPapa's style-guide. It is modular and helps you separate your concerns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.