0

I am trying to use Angular JS service for various purpose. I already made functions inside main script.js but want to shift in employeeService.js service file. Inside it, I am trying to implement a Delete functionality,Here is code-

/// <reference path="script.js" />
app.factory('fetchEmpService', function () {
    var deleteEmp = function (EID) {
        if (confirm("Are you sure want to delete?")) {
            $http({
                method: "POST",
                url: 'EmpWebService.asmx/DeleteEmployee',
                data: { EmpId: EID },
                headers: { 'Content-Type': 'application/json; charset=utf-8' }
            })
        .then(function (reponse) {
            alert("Deleted successfully.");
            $scope.getEmployee();
        });
        }
    }
    return {
        deleteEmp:deleteEmp,
    };
});

And in my main script.js file-

 $http.get("EmpWebService.asmx/GetEmp")
        .then(function (response) {
            $scope.employees = response.data;
        });

The sevice is running and control goes inside it but its throwing following error- angular.js:5582 ReferenceError: $http is not defined.

Similarly I was trying to call a method of fetching EmployeeList function its giving error. What may be the reason? Is there any issue regarding web service that I am using?

1
  • $scope is not injectable in factory or service Commented Jun 1, 2016 at 12:26

1 Answer 1

1

You need to inject $http into your factory. You then want to return the promise and update the $scope inside a closure inside your controller

app.factory('fetchEmpService', function ($http) {
    ....
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks,that was the reason.
also $scope.getEmployee() function would not work inside factory, as $scope can not be get injected in factory
Can we inject $scope also in function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.