Search code examples
angularjsrequirejsangularjs-service

Using angularjs service ($cookies) in factory registered using couchpotato


I'm using angularjs with couchpotato for help with lazy loading. My question is how do I reference angularjs services like $http, $cookies in my service registered using couchpotato?

The normal angularjs way:

factory('MyService', function($cookies) {
  $cookies.message = "hello";
});

How do I do the above using angularjs with couchpotato.js? Below is my service with couchpotato:

define(['app'], function(app) {
  app.couchPotato.registerFactory(['myFactory',
    [
      function() {
        var factory = {};

        factory.registerCookie = function(){
          $cookies.message = 'hello';
        };

        return factory;
      }
    ]
  ]);
});

Of course the above wont work because I have no reference to $cookies in the factory.

Although the above example is specific to $cookies it is relevant to all angular services like $http, $rootScope etc.

Link to couchpotato.js: LINK


Solution

  • This is off the top of my head, but something like this

    define(['app'], function(app) {
      app.couchPotato.registerFactory(['myFactory',
        [ '$http', '$cookies',
          function($http, $cookies) {
            var factory = {};
    
            factory.registerCookie = function(){
              $cookies.message = 'hello';
            };
    
            return factory;
          }
        ]
      ]);
    });
    

    You could also look at this sample https://github.com/afterglowtech/angular-couchPotato/blob/master/samples/components-demo/js/lazy/controllers/myCtrl1.js which injects $scope