0

Sorry friends,I put wrong code and correcting here. I am trying to $scope in my Angular service but as obvious giving error-

ReferenceError: $scope is not defined

Here is my code in service-

 app.factory('fetchEmpService', function ( $scope,$http) {
        var editEmp = function (EID) {
                debugger;
                for (i in $scope.employees) {
                    if ($scope.employees[i].EmpId == EID) {
                        $scope.newemployee = {
                            EmpId: $scope.employees[i].EmpId,
                            Name: $scope.employees[i].Name,
                            Age: $scope.employees[i].Age,
                            City: $scope.employees[i].City,
                            Gender: $scope.employees[i].Gender
                        };
                    }
                }
            }


        return {
                editEmp:editEmp
               }

    }

And in my controller I am trying to consume service like this-

 $scope.EditEmployee = function (EID) {
            debugger;
            $scope.employees = fetchEmpService.editEmp(EID);
        }

I googled about it and found that we can not inject like this. But not found any appropriate solution.

8
  • 2
    You don't inject $scope into a service. Commented Jun 2, 2016 at 9:46
  • Ok but what is the way to do it? Commented Jun 2, 2016 at 9:58
  • 1
    Your definition of the service has .controller. Are you sure its the correct one you wanted to paste? Commented Jun 2, 2016 at 10:00
  • No,aditya-singh actually it is in main js file where I am consuming service. Commented Jun 2, 2016 at 10:06
  • @RiteshGupta i agree with Aditya. You have a function called editEmp which you are defining in the controller - which is called EmpCtrl. Either you are defining them incorrectly or both have the same name. You need to clarify what you're trying to do or build a plunkr so we can tinker Commented Jun 2, 2016 at 10:11

1 Answer 1

0

Refer this link for understanding how to pass parameters to service

I think $scope.employees is an array of employees.

In the service you are trying to find out the details of a particular employee and return the details of this employee back to the controller.

If this is your requirement,

then, I think most probably you can access $scope.employees with the controller.

If it is possible with in the controller you can pass $scope.employees as a parameter to service.

Within the service we cant access $scope.

Refer this to find a particular employee details from a json array of employees

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.