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!
Already researched the idea of "Factories" and "services" of Angularjs?
– Gabriel Katakura