Best way to transfer data between ngRoute controllers (at a glance to the next)

I am wondering what is the best (for Angular way) data transfer between views displayed on a particular route, and I would like to avoid data transfer by URL, as there may be a lot of it or it will require some kind of conversion to strings, etc. In general, this is not a good idea.

An example to understand this:

I have a new content view that displays with empty fields, and users can enter all the necessary data. But in some cases, I would like some of these fields to be pre-populated with data based on what users did in the previous view.

Each view has its own controller. Can communication through services possibly still work? Is there any other way and is easier, so we do not need to worry about the lifetime (i.e. if we speak through the service, we must manually discard the data after reading).

I am wondering what Angular suggested a way to do this?

+3
source share
1 answer

Yep, service or factory will be ok for this case. You can also save this previously entered data in local storage, for example, using this service. This is basically the best way to improve in the future.

As an easier way, you can simply use $ rootScope. It is available in all of your controllers that you want.

.

app.controller('myCtrl', function($rootScope){
    $rootScope.myStoredValues = {...};
});

app.run(function($rootScope){
    $rootScope.root = $rootScope; 
    $rootScope.myStoredValues = {...};
    // this way you don't need to inject it into controller, 
    // it will be available there via $scope.root.myStoredValues
});
+2

All Articles