Question

I'm fairly new to AngularJS and I realise this question has been asked before (so hopefully I wont get voted down) but I've been unable to find a question matching my situation.

I want to pass search data (about 4 fields) from a search page (My HomeCtrl) to a ListingCtrl. The listingCtrl already uses a factory/service to get info based on whats passed from an API (I've hardcoded the data at present).

Now everything I read says uses another server/facotry to pass the data between the search page and the listing page but (and forgive me if I'm missing something) why not just pass these additional params as routeParams??

Incase you need to know - I'm using partials and route params to navigate around the app and pass a few simple IDs between pages.

Was it helpful?

Solution

I suggest to use a service.

If you want stock persistant data, i suggest to use this https://github.com/Zmetser/localstorageservice

This module uses localStorage (documentation) (or cookies if you use IE)

Untested example:

The service

myApp.service('controllerSharingData', function() {
  var __variables = {};

  return {
   get: function(varname) {
    return (typeof __variables[varname] !== 'undefined') ? __variables[varname] : false;
   },
   set: function(varname, value) {
    __variables[varname] = value;
   }
  };
});

Controllers

myApp.controller('IndexCtrl', function($scope, controllerSharingData) {
  controllerSharingData.set('toto', 'hello world');
});

myApp.controller('ListCtrl', function($scope, controllerSharingData) {
  alert(controllerSharingData.get('toto'));
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top