AngularJS: Return values from function
How can I resolve this problem?.
function byDepartment () {
var result = null;
deparmentService.query({
codDepartment: vm.codDepartment.codDepartment,
reportBycodDepartment: 1
}, function (data) {
var success = JSON.stringify(data.success);
if (success === false) {
console.log(JSON.stringify(data.data));
result = data.message;
} else {
result = data.data;
console.log('Always has a value: ' + result);
}
});
console.log('Always null: ' + result);
return result;
}
The console of the Chrome answer this: Always null: null Always has a value: [object Object],[object Object]
I would like to use this value in the next function after:
vm.updateChart = function () {
vm.testValue = 0;
if (!vm.codTestRoom) {
console.log('consulta con departamento');
vm.testValue = byDepartment();
} else if (!vm.codEquipment) {
console.log('Consulta con departamento y testRoom');
vm.testValue = byDepartmentAndTestRoom();
} else {
console.log('Consulta con departamento, testRoom y Equipo');
vm.testValue = byDepartmentAndTestRoomAndEquipment();
}
// console.log('Valor: ' + JSON.stringify(vm.testValue));
};
After this with vm.testValue I'm going to draw a chart with chart.js..
Thanks.