Passing Complex Parameters between Angular Controllers

Last week I encountered for the first time the need to pass complex parameters from one Angular controller to another.  The basic scenario is related to passing selected date and other criteria data from a view with a calendar on it to another view / URL where an appointment is being edited.  I want to have appointment editor it as a separate view and controller because I like to keep my controllers small and because I want to have a specific URL for an appointment to support deep liking of URLs.  One idea is to pass all parameters as URL parameters using $route service.  I started heading this route, but I found this solution inelegant.  URL looked big and strange.  So, I started thinking about doing it another way.  So, a simple answer that I came up with was to use a service.  The reason this works is because services in Angular are singleton.  So, once an instance is created, it will be injected into all controllers that request this service.  I simply add a field into the service.  My first controller will set this field to a complex object, such as

service.parameterField = {

  field1: “value1”,

  field2: “value2”




Then in second controller i can simply get the value of that field.  After that I null it out to limit the possibility of passing incorrect value second time around.  There are alternative solution, of course.  You could use $rootScope or $cache service.  However, both of these smell a little to me because $rootScope is not designed for that and and cache service should not really handle parameters either.  Please let me know what you think about this simple answer.


Thanks and enjoy.

One Comment

  1. Sounds good Sergey. I put together a practical example (albeit rushed) here:

    If I had a few more minutes I’m sure it could be improved upon. I’ve worked some of the other routes with this problem (specficially eventing) and you almost always have to do $scope.$apply(). You then get into two synchronization issues if you have multiple events that you respond to that can be triggered simultaneously.

Leave a Reply

Your email address will not be published. Required fields are marked *