简体   繁体   English

我应该如何在AngularJS中制作可配置的模块

[英]How should I make configurable modules in AngularJS

I've been tinkering with AngularJS and I've built up a small collection of directives and services that I would like to package into a single JS file so that I can use them anywhere. 我一直在修改AngularJS,我已经建立了一小部分指令和服务,我想把它打包成一个JS文件,以便我可以在任何地方使用它们。

I have some website specific settings that my module will need for API calls and that sort of thing. 我有一些网站特定的设置,我的模块将需要API调用等等。 I'm just wondering what the Angular way of making configurable modules is. 我只是想知道Angular制作可配置模块的方法是什么。 Obviously I don't want to have to modify my reusable JS file for each website, as that kind of defeats the purpose of having it. 显然,我不想为每个网站修改我的可重用JS文件,因为这样做会破坏它的目的。 Seeing as the values are going to remain the same for each website it seems like an awful lot of hassle to pass them in as an argument on each function call, and I'd rather stay away from global variables as much as possible. 看到每个网站的值保持不变,将每个函数调用作为参数传递它们似乎是一件非常麻烦的事情,而且我宁愿尽可能远离全局变量。

I've searched a lot of questions for the answers I seek, and the closest pattern I've found so far is to have my reusable module be dependant on a not included module called "settings" or something and then define that module in the page's JS file, allowing the reusable module to pull the values from it. 我已经搜索了很多问题以寻找我所寻求的答案,而我到目前为止找到的最接近的模式是让我的可重用模块依赖于一个名为“settings”之类的未包含的模块,然后定义该模块。页面的JS文件,允许可重用​​模块从中提取值。 Here's an example to show what I mean. 这是一个展示我的意思的例子。

This seems backwards to me. 这似乎是我的倒退。 It's kind of like having a function pull values from global values instead of passing the values in as arguments. 这有点像从全局值中获取函数pull值而不是将值作为参数传递。

Is this really the best way of doing this, or is there an alternative? 这真的是最好的方法吗,还是有替代方案?

It sounds like you're looking for a provider . 听起来你正在寻找提供者

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. 仅当您要为应用程序范围的配置公开API时才应使用Provider配方,该API必须在应用程序启动之前进行。 This is usually interesting only for reusable services whose behavior might need to vary slightly between applications. 这通常仅适用于可重用服务,其行为可能需要在应用程序之间略有不同。

Here's a very basic example of a provider: 这是提供者的一个非常基本的例子:

myMod.provider('greeting', function() {
  var text = 'Hello, ';

  this.setText = function(value) {
    text = value;
  };

  this.$get = function() {
    return function(name) {
      alert(text + name);
    };
  };
});

This creates a new service, just like you might with myMod.service or myMod.factory , but provides an additional API that is available at config time—namely, a setText method. 这将创建一个新服务,就像您使用myMod.servicemyMod.factory ,但提供了一个在配置时可用的附加API,即setText方法。 You can get access to the provider in config blocks: 您可以在config块中访问提供程序:

myMod.config(function(greetingProvider) {
  greetingProvider.setText("Howdy there, ");
});

Now, when we inject the greeting service, Angular will call the provider's $get method (injecting any services it asks for in its parameters) and gives you whatever it returns; 现在,当我们注入greeting服务时,Angular会调用提供者的$get方法(在其参数中注入它要求的任何服务),并为您提供它返回的任何内容; in this case, $get returns a function that, when called with a name, will alert the name with whatever we've set with setText : 在这种情况下, $get返回一个函数,当使用名称调用时,将使用setText设置的任何内容警告名称:

myMod.run(function(greeting) {
  greeting('Ford Prefect');
});

// Alerts: "Howdy there, Ford Prefect"

This is exactly how other providers, like $httpProvider and $routeProvider work. 这正是其他提供商,如$httpProvider$routeProvider工作方式。

For more information on providers and dependency injection in general, check out this SO question on dependency injection . 有关提供程序和依赖项注入的更多信息,请查看有关依赖项注入的SO问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM