javascript - Overriding AngularJs Service - Stack Overflow

I'm making an AngularJs module. The thing is, this module is suppose to work with other module (it

I'm making an AngularJs module. The thing is, this module is suppose to work with other module (its like a plugin). So I'll basically import this module from another module later on. However, there are some service in this module which I would like to override later on from the module which import this module. How would I do so? Would simply defining another service with the same name do it? If so, how would I get the original service?

I'm making an AngularJs module. The thing is, this module is suppose to work with other module (its like a plugin). So I'll basically import this module from another module later on. However, there are some service in this module which I would like to override later on from the module which import this module. How would I do so? Would simply defining another service with the same name do it? If so, how would I get the original service?

Share Improve this question asked Oct 20, 2014 at 11:15 asdacapasdacap 8382 gold badges10 silver badges16 bronze badges 2
  • 3 Have you checked $provide.decorator? – Yoshi Commented Oct 20, 2014 at 11:18
  • Ah! This will do. Thanks! – asdacap Commented Oct 20, 2014 at 11:36
Add a ment  | 

1 Answer 1

Reset to default 6

The override will occur based on the order of instantiation of the modules. The last module created will have precedence over modules with duplicate services created before it.

SO link regarding Namespacing of services: "Namespacing" services in AngularJS

Plunker : http://plnkr.co/edit/P488AkNtGYGUXmo9gIAT?p=preview

  var dep1 = angular.module("dep1",[]);
  var dep2 = angular.module("dep2",[]);
  var app = angular.module("app",["dep2","dep1"]);

  dep1.factory("helloSrvc",function(){
    return {
      msg: "hello from dep1"
    }
  });

  dep2.factory("helloSrvc",function(){
    return {
      msg: "hello from dep2"
    }
  });

  app.controller("myCtrl", function(helloSrvc,$scope){
    $scope.msg = helloSrvc.msg;
  });

  angular.bootstrap(document,["app"]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744127533a4559685.html

相关推荐

  • javascript - Overriding AngularJs Service - Stack Overflow

    I'm making an AngularJs module. The thing is, this module is suppose to work with other module (it

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信