How to call PHP function in another Angularjs function?

Asked

Viewed 438 times

1

I have the following Function in Angularjs:

angular.module("fluxo").controller("fluxoCtrl", function ($scope, $http) {

var mostraTodasContasEntradas = function () {
    $http.get("php/index.php").success(function (data){

    });
}

//mostraTodasContasEntradas();

});

<?php
require_once "../con/conexao.php";
require_once "../classes/contaEntrada.php";
require_once "../classes/contaSaida.php";

$entrada = new contaEntrada();
$saidas = new contaSaida();

$entrada->mostraContasEntrada($id_empresa);

This Function will call another Function in a php class that needs an id. How can I do that?

  • Do you only want to get the data from php? In this case, a result from a select?

  • Exact @Celsomtrindade.

  • Create the object in index.php and call the method and return the json.

  • I’m already doing this. To posting this part in the description of the post. .

1 answer

3


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?

  • Celsom, try to put this on top of the code I posted, because I don’t know where I should make this correction, from where.

  • Updated the response.

  • I’m still a little lost. I change everything, that’s why you posted?

  • I edited the reply @Gustavosevero

  • Yeah, I get it, I’m gonna try.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.