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 ?

6
  • can u post your controllers? Commented Aug 3, 2015 at 12:14
  • rootScope is not the good way to pass data to another controller. You should use service or factory. Commented Aug 3, 2015 at 12:16
  • @dhavalcengg updated Commented Aug 3, 2015 at 12:18
  • can you tell me how ? @alifirat Commented Aug 3, 2015 at 12:19
  • 1
    @sani Hi check out an answer I gave to a similar question. stackoverflow.com/questions/31744713/… Commented Aug 3, 2015 at 12:23

1 Answer 1

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.

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

2 Comments

for store i should use something like that : Point.setPoint(point);
Yes, you have to include the "Point" factory in your controller definition and then call Point.setPoint(point); and Point.getPoint();

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.