0

I want to pass my data from one to another controller .
here is my functions :

   app.controller('formVacationController', function($scope, $state, $rootScope) {

    // we will store all of our form data in this object
    $scope.formData = {};

    $rootScope.globalAnswers = "";
    var point;

    checkAnswer = function(){

        if($scope.formData.mashhad === "mashhad"){
            point =  50;

        }
        else if($scope.formData.kish === "kish"){
            point =  100;

        }
        else if($scope.formData.shomal === "shomal"){
            point =  70;

        } else if($scope.formData.shiraz === "shiraz"){
            point =  60;

        }

    }

    $scope.nextStep = function(){
        checkAnswer();
        $rootScope.globalAnswers = point;

        $state.go('form.job');
    }

}); 

and in Another :

  var point;
    point = $rootScope.globalAnswers;  

i'm not able to get value from first controller .

Am i missing something ?

Sadeghbayan
  • 1,163
  • 2
  • 18
  • 38

1 Answers1

2

You can use a factory like this :

app.factory('Point', function () {
    var data = {
        point: 0
    };

    return {
        getPoint: function () {
            return data.point;
        },
        setPoint: function (point) {
            data.point = point;
        }
    };
});

And then in your controllers call setPoint(point) to store the point and getPoint() in the other controller to retreive it.

Yoann
  • 446
  • 3
  • 6
  • for store i should use something like that : Point.setPoint(point); – Sadeghbayan Aug 03 '15 at 12:35
  • Yes, you have to include the "Point" factory in your controller definition and then call Point.setPoint(point); and Point.getPoint(); – Yoann Aug 03 '15 at 12:38