Call another controller function at the angle

Asked

Viewed 3,127 times

2

I have this controller:

myApp.controller('listarClientesController', function($scope, $http){

$scope.lista = listarClientes();

function listarClientes() {
    $http({
      method: 'GET',
      url: baseUrl + '/index.php/Clientes/Listar'
    }).then(function successCallback(response) {
        $scope.clientes = response.data;
      }, function errorCallback(response) {
        toastr["error"]("Erro ao obter os registros", "Sistema");
    });
}}); 

I need to call listarClientes after I successfully entered this controller:

myApp.controller('cadastrarClienteController', function($scope, $http)

  $scope.cadastrarCliente = function () {

  $scope.cliente = {
    CliNome: $scope.cliente.CliNome,
    CliTelefone: $scope.cliente.CliTelefone,
    CliEmail: $scope.cliente.CliEmail,
    CliDescricao: $scope.cliente.CliDescricao
   };

var response = $http({
    method: "POST",
    url: baseUrl + "/index.php/Clientes/create",
    data: $scope.cliente,
    dataType: "json"
}).then(function successCallback(response) {
    toastr["success"]("Registro inserido com sucesso!", "Sistema");
  }, function errorCallback(response) {
    toastr["error"]("Erro ao inserir o registro", "Sistema");
});

$scope.lista;

return response;

Edit:

Service:

myApp.service('clienteService', function($scope, $http){

this.listarClientes = function() {
    $http({
      method: 'GET',
      url: baseUrl + '/index.php/Clientes/Listar'
    }).then(function successCallback(response) {
        $scope.clientes = response.data;
      }, function errorCallback(response) {
        toastr["error"]("Erro ao obter os registros", "Sistema");
    });
}});

Edit:

so stated in controller:

myApp.controller('cadastrarClienteController', function($scope, $http, clienteService){

Repair:

myApp.service('clienteService', function($http){
this.ListarClientes = function() {
    return $http.get(baseUrl + '/index.php/Clientes/Listar');
}

});

Thank you!

  • 1

    Already researched the idea of "Factories" and "services" of Angularjs?

1 answer

3


If you are consuming the same data source from multiple places in your application it might be worth implementing your source as a service or Factory, as indicated in the Gabriel:

.factory(
    "clientesFactory", function ($resource) {
        return $resource("/index.php/Clientes/Listar");
    })

And consume this Factory where you need:

myApp.controller('listarClientesController', function($scope, clientesFactory){
    clientesFactory.query(function(data){ $scope.lista = data; });
}
  • Ok... I put the function I need in a service... I edited my question... is giving error in the service.. It’s the way I did it?

  • @beginner could describe the mistake?

  • $scopeProvider <- $scope <- clienteService&#xA;http://errors.angularjs.org/1.5.5/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20clienteService

Browser other questions tagged

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