javascript - how can i pass the data between two functions of a service in angularjs -


i trying create multi step form in have made 2 different views , 2 different controllers trying pass data view 1 function defined in service , trying access data in function of same service. following service 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 trying access emp_email variable of email function personal function. not able so. there way can use emp_email variable in second function.

following controller 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 executed when user fills email , hits on next section , secondsection function executed when user enters first_name in second view , hits submit.

how can pass data between 2 functions in services? sort of appreciated!!!!

the problem code you're creating second "emp_email" variable in e-mail function, email assigning going local emp_email variable within e-mail function rather emp_email variable in service function.

so remove var , 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 share emp_email variable between functions.


Comments