GET function in ng-click

3

I need to call a function GET through ng-click to populate a table and the function without using service works, but the function using service does not work.

Controller:

It works

$scope.carregarContratos = function() {
    $http({method: 'GET', url: config.baseUrl + '/contratos'})
    .then(function(response) {
        $scope.contratos = response.data;
    }, function(response) {
        console.log(response.data);
        console.log(response.status);
    });
};

Does not work

carregarContratosPaginados = function (pageNumber) {
    ContratosAPI.getContratosPaginados(pageNumber, $scope.usersPerPage).success(function (data) {
        $scope.contratos = data;
        $scope.totalContratos = 100;
    }).error(function (data, status) {
        $scope.message = "Aconteceu um problema: " + data;
    });
};

Service:

angular.module("myapp").factory("ContratosAPI", function ($http, config) {
    var _getContratos = function () {
        return $http.get(config.baseUrl + "/contratos");
    };

    var _getContratosPaginados = function (pagina, total) {
        return $http.get(config.baseUrl + "/contratos?pagina=" + pagina + "&total=" + total);
    };

    var _saveContrato = function (contrato) {
        return $http.post(config.baseUrl + "/contratos", contrato);
    };

    return {
        getContratos: _getContratos,
        getContratosPaginados: _getContratosPaginados,
        saveContrato: _saveContrato
    };
});

ng-click:

ng-click="carregarContratosPaginados(1)"
    
asked by anonymous 18.11.2016 / 11:51

1 answer

4

$scope is missing before your method declaration to be visible to the view:

$scope.carregarContratosPaginados = function (pageNumber)...
    
18.11.2016 / 12:43