在使用 couchpotato 注册的工厂中使用 angularjs 服务($cookies) [英] Using angularjs service ($cookies) in factory registered using couchpotato

查看:23
本文介绍了在使用 couchpotato 注册的工厂中使用 angularjs 服务($cookies)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 couchpotato 的 angularjs 来帮助延迟加载.我的问题是如何在使用 couchpotato 注册的服务中引用 angularjs 服务,例如 $http、$cookies?

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?

正常的angularjs方式:

The normal angularjs way:

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

如何使用带有 couchpotato.js 的 angularjs 执行上述操作?以下是我的沙发土豆服务:

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;
      }
    ]
  ]);
});

当然上面的方法是行不通的,因为我在工厂里没有提到 $cookies.

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

虽然上面的例子是特定于 $cookies 的,但它与所有 Angular 服务相关,如 $http、$rootScope 等.

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

couchpotato.js 的链接:LINK

Link to couchpotato.js: LINK

推荐答案

这让我很头疼,但就像这样

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;
      }
    ]
  ]);
});

您还可以查看此示例 https://github.com/afterglowtech/angular-couchPotato/blob/master/samples/components-demo/js/lazy/controllers/myCtrl1.js 注入 $scope

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

这篇关于在使用 couchpotato 注册的工厂中使用 angularjs 服务($cookies)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆