在不同的模块不同的配置Angularjs提供商 [英] Angularjs provider with different configurations in different modules

查看:190
本文介绍了在不同的模块不同的配置Angularjs提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有使不整个应用程序唯一提供者的结构的方法,但仅跨越所配置它的模块

I'd like to know if there is a way of making the configuration of a provider unique not across the entire application, but only across the module that is configuring it.

例如,我有一个模块的核心,其中包含一个提供者的定义。然后我有使用供应商,但必须配置它在特定的模块一种特定的方式模块模块1和模块2。

For example, I have a module "core" which contains the definition of a provider. Then I have modules "module1" and "module2" which use the provider but have to configure it in a particular way for the specific module.

什么是与我发生的事情是最后定义的模块中完成的配置覆盖,在应用程序中使用提供所有其它模块进行配置。

What is happening with me is that the configuration done in the module defined last overrides the configuration done in all other modules that use the provider in the application.

我做了这个简单的plunker来体现这一点:

I made this simple plunker to exemplify this:

var app = angular.module('app', ['module1','module2']);

angular.module('core',[])

.provider('movie', function () {
  var version;
  return {
    setVersion: function (value) {
      version = value;
    },
    $get: function () {
      return {
          title: 'The Matrix' + ' ' + version
      }
    }
  }
});

angular.module('module1',['core'])

.config(function (movieProvider) {
  movieProvider.setVersion('Reloaded');
})

.controller('ctrl1', function ($scope,movie) {
  $scope.title = movie.title;
  $scope.module = 'module1';
});


angular.module('module2',['core'])

.config(function (movieProvider) {
  movieProvider.setVersion('Revolutions');
})

.controller('ctrl2', function ($scope,movie) {
  $scope.title = movie.title;
  $scope.module = 'module2';
});

http://plnkr.co/edit/BI5nhmcdgzQZfZo56Koh?p=$p$ PVIEW

推荐答案

在角提供商,$ get是打算返回的功能提供一个实例; $ get()方法的供应商(如工厂)的每个实例被重新执行。提供的提供程序的定义的功能或对象只实例化一次(如服务)。

Theory and other useless crap

In angular providers, $get is intended to return a single instance of the provided functionality; $get() is re-executed on each instantiation of the provider (like a factory). The function or object providing the definition of the provider is only instantiated once (like a service).

因此​​,要获得工厂般的性能你的供应商你要保持特定实例的属性范围内获得$;并获得服务类物业的范围之外$得到。

So, to get factory-like properties out of your provider you'd want to keep instance-specific properties scoped inside of $get; and to get service-like properties scoped outside of $get.

这似乎有点限制或迂回在第一,但它可以让你在真正漂亮的方式实例作用域和全球范围的特性结合起来。

This may seem a bit limiting or roundabout at first, but it allows you to combine instance-scoped and globally-scoped properties in really nifty ways.

要你的问题具体地讲,如果我们只是移动 VAR版本 $ GET ,突然间我们得到的出厂类似的行为你要找的:

To your problem specifically, if we just move var version into $get, suddenly we get the factory-like behavior you were looking for:

.provider('movie', function () {
  return {
    $get: function () {
      var version;

      var obj = {
          title: function() { return 'The Matrix' + ' ' + version; },
          setVersion: function (value) {
            version = value;
          }
      };

      return obj;
    }
  }
});

http://plnkr.co/edit/0m9qKzNuhCOOXTbrCQ1T?p=$p $ PVIEW

(具有变化的标题是一个变量而不是在创建obj的静态生成的字符串的函数沿)改变的范围内,得到我们所希望的输出:

Changing the scope (along with changing title to be a function of a variable instead of a string generated statically at the creation of obj), gets us the desired output:

This is module module1

Title: The Matrix Reloaded

This is module module2

Title: The Matrix Revolutions

这基本上就像一个工厂ATM。

This is basically like a factory ATM.

要进入一个提供商(在这里我们要结合全球和每个实例作用域)的一个比较正常的使用情况下,我们说,我们希望有一个使用可能会使用不同民族的几种不同的API一个人的API提供商应用程序 - 但每个应用程序只有一个API。我们会希望我们的供应商,大致是这样的 -

To get into a more normal use case of a provider (where we want to combine global and per-instance scoping), let's say we wanted to have an API provider that uses may use one of several different APIs in different peoples' applications - but only one API per application. We'd want our provider to look roughly like this -

.provider('movie', function () {
  var imdbEndpoint;

  return {
    setApiEndpoint: function(newImdbEndpoint) {
      imdbEndpoint = newImdbEndpoint;
    },
    $get: function () {
      var version;

      var obj = {
          title: function() { return imdbEndpoint + ' says the The Matrix version is ' + ' ' + version; },
          setVersion: function (value) {
            version = value;
          }
      };

      return obj;
    }
  }
});

http://plnkr.co/edit/ZoSgQDpNdX8MOmet7xzM?p=$p $ PVIEW

输出:
    这是模块模块1

Output: This is module module1

Title: api.imdb.com says the The Matrix version is Reloaded

This is module module2

Title: api.imdb.com says the The Matrix version is Revolutions

所以,现在我们的API端点URL是全局范围(或​​在每个应用程序),但我们可以参考我们要取其电影没有得到混淆有关的基质层新中 - 因为这是这是真正关心,对吧?

So now our API endpoint URL is scoped globally (or on a per-application basis) but we can reference whichever movie we want without getting confused about which layer of the matrix Neo is in - because that's what this is really about, right?

这篇关于在不同的模块不同的配置Angularjs提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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