O
O
okuznetsov12017-03-15 16:02:11
Angular
okuznetsov1, 2017-03-15 16:02:11

How to pass parameters from service to controller?

Implemented a service for adding and receiving data from a table. After adding an entry to the table, you need to organize the transfer of a parameter from the service to the controller. This parameter will signal to the controller that the data was successfully added, and accordingly the controller can push. If not the right one I propose a solution, then tell me how to implement it correctly?
service.dataByCommands.js:

(function(){
   'use strict';

    angular
        .module('service.dataByCommands',[])
        .factory('serviceDataByCommands', serviceDataByCommands);

        function serviceDataByCommands($http) {

            var _vm;
            
            return {
                name: 'Commands Service',
                add: function (vm) {
                    
                    _vm = vm;
                    
                    $http.post('addData.php',{genCommand:vm.newGenerationCommand, description:vm.newDescription})
                        .then(function(response) {

                            RETURN ('SUCCESS');
                            
                        })
                        .catch(function(error) {
                            console.log(error);
                        });                    
                    
                },
                get: function (callback) {
                        $http.get('getData.php')
                            .then(function(response) {
                                callback(response.data);
                            },
                            function(error) {
                                console.log(error);
                            });
                }
            }
            
        }
        
})();

controllers.js:
var vm = this;
.....

serviceDataByCommands.get(function(data){
     vm.lists = data;
});
        
vm.addCommand = function() {
    var status = serviceDataByCommands.add(vm);

    if (status === 'success'){

                            vm.notify('Данные успешно добавленны!');

                            vm.lists.push({
                                    id: response.data.id,
                                    command: response.data.command,
                                    description: response.data.description,
                                    completed: response.data.completed
                            });

   }

};

.....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
okuznetsov1, 2017-03-15
@okuznetsov1

in service:

function serviceDataByCommands($http) {
            
            return {
                name: 'Commands Service',
                add: function (vm) {

                    return $http.post('addData.php',{genCommand:vm.newGenerationCommand, description:vm.newDescription});     
                        
                },
                ...........................

in controller:
vm.addCommand = function() {
serviceDataByCommands
.add(vm)
.then(function(response) {
vm.notify('success','Command "' + response.data[0].command + '" successful saved!');
vm.lists.push({
id: response.data[0].id,
command: response.data[0].command,
description: response.data[0].description,
completed: response.data [0].completed
});
})
.catch(function(error) {
console log(error);
});
};

S
SergeyBugai, 2017-03-15
@SergeyBugai

Doing in the service

return  $http.get('getData.php')
                            .then(function(response) {
                                return response.data;
                            },
                            function(error) {
                                console.log(error);
                            });

and in the controller
var status = serviceDataByCommands.add(vm);
status.then(function(response){

    if (response=== 'success'){

                            vm.notify('Данные успешно добавленны!');

                            vm.lists.push({
                                    id: response.data.id,
                                    command: response.data.command,
                                    description: response.data.description,
                                    completed: response.data.completed
                            });

   }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question