Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: $http.get is not a function in Angular JS

Tags:

angularjs

I am trying to use Angular Service and since $scope can not be injected inside service so using $rootScope. My code look like fine but getting following error-

TypeError: $http.get is not a function

Here is code- EmployeeService.js: ///

app.factory('fetchEmpService', ['$rootScope', '$http', function ($http, $rootScope) {
    var employees = [];
    return {
        fetchEmp: function () {
            debugger;
            return $http.get("EmpWebService.asmx/GetEmp")
                .then(function (response) {
                    employees = response.data;
                    $rootScope.$broadcast('allEmployees', employees);
                    return employees;
                });
        }

    };
}]);

In my controller I am trying to consume it like below:

$scope.employees = fetchEmpService.fetchEmp();
$scope.$on('allEmployees', function (events, employees) {
        $scope.employees = employees;
    });

I am bit confuse that will the data will come inside $scope.$on

like image 596
Ritesh Gupta Avatar asked May 31 '26 21:05

Ritesh Gupta


1 Answers

Your parameters and array of injectables are in a different order.

This:

app.factory('fetchEmpService', ['$rootScope', '$http', function ($http, $rootScope)

Needs to be

app.factory('fetchEmpService', ['$rootScope', '$http', function ($rootScope, $http)

The order is important and needs to be the same.

More information on dependency injection.

like image 91
Matt Lishman Avatar answered Jun 02 '26 20:06

Matt Lishman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!