Reputation: 18939
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
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
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