Alan2
Alan2

Reputation: 24562

Can I pass data to a state with ui-router?

I have this call to a state:

self.$state.go('heat',
                {
                    subjectId: self.sus.subject.id,
                    examId: self.exs.exam.examId,
                    testId: self.test.testId,
                    questionNumber: 1
                })

Is there a way that I can pass an array of data to this state and if so how could I get that data in my router config file?

Upvotes: 1

Views: 74

Answers (2)

Satej S
Satej S

Reputation: 2156

As the answer by Mike suggests, you can add params in the config. I'd just like to elaborate on that.

In your app.js, you should specify the parameters you are sending

.state('statename',{
        url:'/state',
        templateUrl:'js/apps/xPage/templates/something.html',
        params:{'parameter1':null,'parameter2':null},
        controller:'stateCtrl'

    });

In your controller, you can define your data like this

 $scope.parameter1=[];
 $scope.parameter2=[];

You can add to your array like this

$scope.parameter1.push(x);
$scope.parameter2.push(y);

Finally, send your values like this

$state.go('results',{'parameter1':$scope.parameter1,'parameter2':$scope.parameter2});

Upvotes: 1

Mike Jerred
Mike Jerred

Reputation: 10515

You can add params in the state config, and specify the values in $state.go. You can then access these values with $stateParams service.

UI Router Docs

Upvotes: 0

Related Questions