1
I’m trying to return an array of a function to assign to a model
in Angularjs. However the return of the function is undefined
, but when I give one console.log
before returning the array is correct. How do I return the array clientes
and assign to the model
$scope.clientes
?
angular.module('myApp')
.controller('myCtrl', function($scope, $http){
$scope.selected = undefined;
$scope.clientes = getClientes($http);
console.log(getClientes($http)); //O retorno aqui é undefined
});
function getClientes($http){
$http.get('my/url')
.then(function(response){
var clientesJson = JSON.parse(response.data);
var clientes = new Array();
for(var i = 0; i < clientesJson['clientes'].length; i++){
clientes.push(clientesJson['clientes'][i]['nome']);
}
console.log(clientes);//Aqui é mostrado o array corretamente
return clientes;
});
}
And what is the return of the function
getClientes
? Thereturn
used is from the anonymous function passed to thethen
.– Woss
I managed to solve by putting the
return clientes
after thethen
.– Diego Soares