In that case, I recommend that you create a factory
I mean, a service. This way, whenever you need this data, whether in this or another controller, you can only refer to it in an easier and modular way.
Would something like this:
.factory('factCliente', ['$http', function($http) {
//esse aqui faz um get simples
var _getData = function() {
return $http.get("data/index.php");
};
//esse aqui faz o carregamento do dado enviando algum atributo para o php processar
var _getData2 = function(id) {
return $http.post("data/index.php", id);
};
return {
getData: _getData,
getData2: _getData2,
}
}])
And then to call these functions in your controller, you would do so:
.controller("fluxoCtrl", function ($scope, $http, factCliente) {
var mostraTodasContasEntradas = factCliente.getData();
//Ou então
var idCliente = {id: meuId},
mostraTodasContasEntradas = factCliente.getData2(idCliente);
});
So, if you need customer data in another control, just inject the Factory of it, ie factCliente
in your new controller, and call the function. Example:
.controller('OutroCtrl', function($scope, factCliente) {
$scope.maisCliente = factCliente.getData();
})
Edit:
The idea of this is that you nay use your $http inside the controller, because let’s imagine the following situation:
You have 7 controllers that do this same php get. If by any chance you change the name of php, or change its structure so that you need to change in your Angularjs, you will NOT need to change in 7 different places, being prone to more errors.
The way you did it works, just to GET the raw data, let’s say it’s a simple php select, through the first example ( _getData): select * from 'tabela'
, that is, will take all the data.
The second method (the _getData2) I showed it uses the POST, because first you send the data and then select what you need, example: select from 'tabela' where id = $meuId
.
You’ve come to understand the logic?
Do you only want to get the data from php? In this case, a result from a select?
– celsomtrindade
Exact @Celsomtrindade.
– GustavoSevero
Create the object in index.php and call the method and return the json.
– rray
I’m already doing this. To posting this part in the description of the post. .
– GustavoSevero