jviotti
jviotti

Reputation: 18939

Pass information from view to view

I have a multistep form where information from the first steps should be available to the next ones.

How do I share this information between views? Should I append the models in the $rootScope or create a common parent controller?

Upvotes: 0

Views: 49

Answers (2)

nordyke
nordyke

Reputation: 113

Create a service to store the shared items, since services are singletons. Here's an arbitrary example.

sharedDataExampleModule.service('SharedData', function(){
    var data = {};
    // API to get/set shared data
    return {
        getData: function(){},
        setData: function(data){}
    };
});

Inject the service into any controller that needs to access the data.

sharedDataExampleModule.controller('ControllerA', function($scope, SharedData){
    // set data
    SharedData.setData($scope.objectContainingOurSharedData);
});

sharedDataExampleModule.controller('ControllerB', function($scope, SharedData){

    // get data
    $scope.shared = SharedData.getData();
});

Upvotes: 2

Tiago Barreto
Tiago Barreto

Reputation: 822

First View Controller, passing the object param:

function FirstViewController($scope) {
    $http.post(url).success(function(objModel){
        $scope.objModel = objModel;
    }).error(function(msg){
        console.log(msg);
    });
}

Second View Controller, get the params:

function SecondViewController($scope, $params) {
    var objModel = $params.objModel;
}

I hope to help you.

Upvotes: 1

Related Questions