AngularJS : How to watch service variables?

You can always use the good old observer pattern if you want to avoid the tyranny and overhead of $watch. In the service: factory(‘aService’, function() { var observerCallbacks = []; //register an observer this.registerObserverCallback = function(callback){ observerCallbacks.push(callback); }; //call this when you know ‘foo’ has been changed var notifyObservers = function(){ angular.forEach(observerCallbacks, function(callback){ callback(); }); … Read more

angular.service vs angular.factory

angular.service(‘myService’, myServiceFunction); angular.factory(‘myFactory’, myFactoryFunction); I had trouble wrapping my head around this concept until I put it to myself this way: Service: the function that you write will be new-ed: myInjectedService <—- new myServiceFunction() Factory: the function (constructor) that you write will be invoked: myInjectedFactory <— myFactoryFunction() What you do with that is up to … Read more

Passing data between controllers in Angular JS?

From the description, seems as though you should be using a service. Check out http://egghead.io/lessons/angularjs-sharing-data-between-controllers and AngularJS Service Passing Data Between Controllers to see some examples. You could define your product service (as a factory) as such: app.factory(‘productService’, function() { var productList = []; var addProduct = function(newObj) { productList.push(newObj); }; var getProducts = function(){ … Read more