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

旧城冷巷雨未停 提交于 2019-12-13 03:12:34

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/17354093/using-angularjs-service-cookies-in-factory-registered-using-couchpotato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!