Actually your problem must be happening because you are NOT returning the search result as an array, but rather as a simple sql result, ie the angle will be able to read, but it would not be possible to transcribe to a ng- repeat (or other treatment), since you are creating an 'invalid' json object.
What you should do is create a php that can transcribe your result in the following format:
[
{id:1,cliente:'nome do cliente 1',estado_civil:'solteiro'..},
{id:2,cliente:'nome do cliente 2',estado_civil:'casado'..},
{id:3,cliente:'nome do cliente 3',estado_civil:'viuvo'..},
[.. etc ..]
]
For this you can use code similar to this:
function usuarios(){
$data = array();
$qry = sql("select * from users");
$i=0;
foreach($qry as $r) {
foreach ($r as $j=>$k) {
if(!is_int($j)){
$data[$i][$j] = $k;
}
}
$i++;
}
return json_encode($data);
}
I recommend caution when making this get, especially with regard to creating a json file. From the moment you write a new json and save it to the server, it will be available to anyone who can access your folder structure.
With the need for an sql to get the data, your application gets more secure.
In the part of your AngularJs, it is actually preferable that you use a function with var
instead of $scope
to obtain this data. For creating a new $ scope, you increase the possibility of errors, since it creates interaction with DOM and increases the need for AngularJs checks.
The only detail of this is that your php result will look like this:
data: [
{id:1,cliente:'nome do cliente 1',estado_civil:'solteiro'..},
{id:2,cliente:'nome do cliente 2',estado_civil:'casado'..},
{id:3,cliente:'nome do cliente 3',estado_civil:'viuvo'..},
[.. etc ..]
]
Then your code in AngularJs, should return the array DATA
, thus:
var carregarUsuarios = function () {
$http.get("php/buscar.php").then(function (retorno){
$scope.usuarios = retorno.data;
});
};
Notice the definition of $scope.usuarios
, I say that I want to assign the 'return' and then call the 'array', that is, retorno.data
.
Complementing still more, if you want to use multiple functions in php, just do the following:
//utilize a url da seguinte maneira
$http.get("php/buscar.php?action=carregarUsuarios")
//ou chamando outra função
$http.get("php/buscar.php?action=maisFuncao")
And the Php:
switch($_GET['action']) {
case 'carregarUsuarios': carregarUsuarios();
break;
case 'maisFuncao': maisFuncao();
break;
}
function carregarUsuarios() {
//sua função aqui
}
function maisFuncao() {
//sua função aqui
}