2
I’m having a problem when I make a GET request to the server. My Factory always returns an empty object, when it should not.
So mine server (localhost:8080/av1/listarCarroEstoque)
upon request return the following JSON:
[
{
"id": 0,
"chassi": "123455",
"montadora": "ford",
"modelo": "alto",
"tipo": "HATCH_MEDIO_ESPORTIVO",
"cor": "ROSA",
"motorizacao": 2500,
"preco": 599
}
]
My file ListarCarroFactory.js
:
app.factory('ListarCarroResource', ['$http', function($http) {
return {
listar: function() {
var dados = {};
$http.get('/av1/ListarCarroEstoque').
success(function(data) {
dados = data;
}).
error(function(error) {
dados = error;
});
return dados;
};
}
}]);
My file ListarCarro.js
:
app.controller('ListarVeiculo', function($scope, ListarCarroResource) {
$scope.opcoes = ['carro', 'moto'];
$scope.veiculoSelecionado = false;
$scope.listarVeiculos = function() {
if ($scope.veiculo == $scope.opcoes[0]) {
console.log( (ListarCarroResource.listar()) );
$scope.veiculoSelecionado = true;
} else {
$scope.veiculoSelecionado = true;
}
};
});
And mine Main.js
:
var app = angular.module('loja', ['ngResource']);
I didn’t understand what the error was, it always returns the same JSON ?? Here it wouldn’t be $http.get('/project/av1/Listinginventory'). ?
– Hiago Souza
The problem is not very clear. Your service in
localhost:8080/av1/listarCarroEstoque
is returning a collection (marked by symbols[
and]
) containing an item (marked by{
and}
). What would be the expected result?– OnoSendai
Sorry, I misspelled the server url is localhost:8080/av1/listCarroEstoque
– Leonardo Villela
When I call the list method from my Factory it returns me an empty object with no data inside, that’s the problem.
– Leonardo Villela
@Leonvillardoela I would suggest adding this last comment to the question then. Another thing - in the code present in the Pastebin there is no mention to the Resource Factory (mentioned in main.js, ngResource module').
– OnoSendai
I already changed thanks @Onosendai.
– Leonardo Villela
Make a fiddle said...
– Tivie