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.

Ritesh Gupta
  • 171
  • 1
  • 2
  • 19

1 Answers1

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

Community
  • 1
  • 1