I'm trying to return an array of a function to assign to a model
in Angularjs. But the function return is undefined
, but when I give console.log
before returning the array is correct. How do I return the array clientes
and assign it to 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;
});
}