Answer the question
In order to leave comments, you need to log in
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);
});
}
}
}
})();
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
in service:
function serviceDataByCommands($http) {
return {
name: 'Commands Service',
add: function (vm) {
return $http.post('addData.php',{genCommand:vm.newGenerationCommand, description:vm.newDescription});
},
...........................
Doing in the service
return $http.get('getData.php')
.then(function(response) {
return response.data;
},
function(error) {
console.log(error);
});
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 questionAsk a Question
731 491 924 answers to any question