1

I am trying to create a multi step form in which I have made two different views and two different controllers I am trying to pass the data from view 1 to function defined in service and then trying to access this data in another function of same service. The following is the service I have written: service.js

var EmployeeService = angular.module('EmployeeService',[])
.service('AddEmployee', function($resource){
     var Employee = $resource('/api/meetups');
        var emp_email="";
    this.email = function(email){
        var emp_email = email;
            return emp_email;
    };

    this.personal = function(first_name){
            var employee = new Employee();
            employee.email = emp_email;
             employee.first_name = first_name;
              employee.$save();
    };
});

I am trying access emp_email variable of email function into personal function. but I am not able to do so. is there any way I can use emp_email variable in my second function.

Following is the controller which is using services:

app.controller('mainCrl', ['$scope', '$resource', '$location', 'AddEmployee', function ($scope, $resource, $location, AddEmployee){
  $scope.nextSection = function(){
  $scope.email = AddEmployee.email($scope.employeeEmail);
  };
}]);
app.controller('secondCrl', ['$scope', '$resource', '$location', 'AddEmployee', function ($scope, $resource, $location, AddEmployee){
   $scope.secondSection = function(){
   $scope.first_name = AddEmployee.personal($scope.first_name);
  };
}]);

nextSection function will be executed when user fills email and hits on next section and secondSection function will be executed when user enters first_name in second view and hits submit.

how can I pass the data between two functions in services? any sort of help is really appreciated!!!!

2 Answers 2

1

The problem with your code is that you're creating a second "emp_email" variable in your e-mail function, so the email you are assigning is going to a local emp_email variable within the e-mail function rather than the emp_email variable in the service function.

So remove the var and change

this.email = function(email){
    var emp_email = email;
        return emp_email;
};

to

this.email = function(email){
    emp_email = email;
        return emp_email;
};

That should allow you to share the emp_email variable between functions.

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

1 Comment

No problem. That was a subtle error that can easily be overlooked. :)
1

Replace

var emp_email = email;

with

return emp_email = email;

(you modify the local variable)

Comments

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.