ChatGPT解决这个技术问题 Extra ChatGPT

AngularJS - Dependency injection in services, factories, filters etc

So I have some plugins and libraries I want to use in my angular app and (currently) I am simply referencing those functions/methods as they were intended in 99% of apps in a way that completely ignores dependency injection.

I have (for example) the javascript library "MomentJS" which deals with formatting and validating dates and I have uses for it throughout my app in controllers, services and filters. The way that I've learned (using AngularJS) is to create a service that references the function (and it's methods) and inject that service into my controllers, which works great.

The problem is that I really need to reference this library in all different kinds of components from services to filters to controllers and everything else. So, I guess my question is how do you do dependency injection in filters, services and everything else that isn't a controller?

Is this possible? Is this even beneficial?

Any help would be greatly appreciated :)


A
Arun P Johny

Yes you can use dependency injection for filters and directives

Ex:

Filter:

app.filter('<filter>', ['$http', function(http){
    return function(data){
    }
}]);

Directive:

app.directive('<directive>', ['$http', function(http){
    return {
        ....
    }
}]);

Service:

app.factory('<service>', ['$http', function(http) {
  var shinyNewServiceInstance;
  return shinyNewServiceInstance;
}]);

Is there any way to bend syntax to inject dependencies into a provider? I need either: a provider that I can inject deps into, or a factory/service that I can inject into a config block(?)
@Cody Services and factories are providers. Also .config() handles Dependency injection in the exact same way as the examples above.
@KevinBeal so for any method of module, (i.e. config, run, controller`, etc), you pass an array with the dependencies and a handler function, and the dependencies will then be injected to the handler function. Am I right? Thank you!
@pilau anywhere a method expects a function you can, yes. You can even do it for the controller method that's in the return value of a directive. (Anywhere there is dependency injection).
@KevinBeal I am asking specifically because at the bottom of this page it says "Passing the dependencies parameter again will wipe out the module definition and start over". So is Mr. Allen wrong?
u
user1338062

For the sake of completeness, here is a service example with injection:

app.service('<service>', ['$http', function($http) {
  this.foo = function() { ... }
}]);

As at this time I seem unable to inject a controller into a service - at least not my own custom one. I really shouldn't be here but I cant see a way to make a modal directive work which sits outside of the controller that instigates it popping up. I created a service to try and do the two way communication thinkingn the service could inject both controllers but alas.. what would be the best way to achieve this. I would like to ask questions here but it seems to be blocked for some reason so I can only comment on similar things.
You really need to make a question, preferably with a plnkr.co of your code.
I would love to be able to write questions - it seems this site once you get blocked for asking too much it blocks you forever. In short I think you cant inject controllers into things (which just sounds daft anyway) So I created service and getter setter and then watch this for changes in the controllers that need to subscribe to the updates. This worked and allows me controller to controller coms.
D
Daniel

While the already existing answers are correct and working, john papas angular style guide favors the use of the $inject service in Y091:

Filter:

app.filter('<filter>', MyFilter);
MyFilter.$inject = ['$http'];
function MyFilter() {
  return function(data) {
  }
}

Directive:

app.directive('<directive>', MyDirective);
MyDirective.$inject = ['$http'];
function MyDirective() {
  return {
    ...
  }
}

Factory:

app.factory('<factory>', MyFactory);
MyFactory.$inject = ['$http'];
function MyFactory() {
  var shinyNewServiceInstance;
  return shinyNewServiceInstance;
}

Service:

app.service('<service>', MyService);
MyService.$inject = ['$http'];
function MyService() {
  this.foo = foo;
  function foo(){
    ...
  }
}

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now