Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular service is not a function error

app.service('situacao', function($log, $q, $http, $rootScope){
    var situacao = this; 
    situacao.lista = {};
    situacao.getAllSituacao = function(){
        var defer = $q.defer();
        console.log("php/getAll.php");
        $http.get($rootScope.endPoint + "php/getAll.php")
            .success(function(res) {
            console.log(res);
            situacao.lista = res;
            defer.resolve(res);
        }).error(function(err, status){
            defer.reject(err);
        }); 
        return defer.promise;
    };
    return situacao;});
    app.controller('listCtrl',['$scope', '$uibModal', '$log', '$http', function(situacao, $scope, $modal, $log, $http) {   
    $scope.init = function(){
        $scope.getAll();
    }
    $scope.getAll = function(){        
        situacao.getAllSituacao().then(function(res){
            //sucess
            $scope.dispSituacao = situacao.lista;  
        }, function(err){
            //error
        })
    };
    $scope.init(); 
}]);

I'm trying to use the "service" but results in error: situacao.getAllSituacao is not a function.

what is wrong?

like image 966
Maicon Putton Avatar asked Dec 08 '22 23:12

Maicon Putton


2 Answers

You have to update your inject to pass it in as well since you're using the array notation:

Change

app.controller('listCtrl', ['$scope', '$uibModal', '$log', '$http', function (situacao, $scope, $modal, $log, $http)

To

app.controller('listCtrl', ['situacao', '$scope', '$uibModal', '$log', '$http', function (situacao, $scope, $modal, $log, $http) {
like image 91
Mathew Berg Avatar answered Dec 12 '22 04:12

Mathew Berg


In my situation, I named all injected services correctly, but, their order was not the same, and it gave me the same error. My code was something like this:

app.controller('listCtrl', ['situacao', '$scope', '$uibModal', '$log', '$http', function ($scope, situacao, $modal, $log, $http) {}

Putting them in correct order solved the problem. Like this:

app.controller('listCtrl', ['situacao', '$scope', '$uibModal', '$log', '$http', function (situacao, $scope, $modal, $log, $http) {}

Hope this helps someone.

like image 21
Khasan 24-7 Avatar answered Dec 12 '22 06:12

Khasan 24-7