工厂使用angularjs服务($饼干),使用couchpotato注册 [英] Using angularjs service ($cookies) in factory registered using couchpotato

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

问题描述

我使用angularjs与couchpotato与延迟加载帮助。我的问题是我怎么引用angularjs服务,如$ HTTP,$使用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?

正常angularjs方式:

The normal angularjs way:

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

我该怎么做上面使用angularjs与couchpotato.js?
下面是我的服务,couchpotato:

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.

虽然上面的例子是特定于$饼干它是适用于所有角度的服务,如HTTP $,$ rootScope等。

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

链接couchpotato.js:链接

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

您也可以看看这个样本<一个href=\"https://github.com/afterglowtech/angular-couchPotato/blob/master/samples/components-demo/js/lazy/controllers/myCtrl1.js\" rel=\"nofollow\">https://github.com/afterglowtech/angular-couchPotato/blob/master/samples/components-demo/js/lazy/controllers/myCtrl1.js其中注入$范围

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

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

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