通过应用程序配置AngularJS动态提供程序选项 [英] dynamic provider options through app config AngularJS

查看:110
本文介绍了通过应用程序配置AngularJS动态提供程序选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接受类似配置提供模块:

I have provider modules that accept configurations like:

angular.module('app', ['search']).
  config(['$searchProvider', function($searchProvider) {
    $searchProvider.options({
      resultLimit:50,
      defaultSort:'highToLow'
    });
  }]);

有将每个客户端一个新的应用实例---所以我想用一个客户自助服务门户配置对象。

There will be a new app instantiation per client --- so I am thinking about using a client self serve portal to configure meta object.

这意味着供应商现在的模块需要坐下的回调方法里面等待才可以设置各自的配置。

This means the provider modules now need to sit inside a callback method to wait for meta before they can set their respective configurations.

但是,让我们都记住:配置块 - 获取在供应商登记和配置阶段执行。只有供应商和常量可以被注入到配置块。这是prevent服务,他们已经完全配置之前的意外实例...

But let's all remember: Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured...

...所以文档说​​,你可以使用配置块供应商,但我不相信你可以用它们来使服务呼叫。因为我不知道如何来处理这个正确的方式,我会告诉你我的高层次的想法:

...So the docs say you can use provider in configuration blocks, but I'm not sure you can use them to make service calls. Because I have no idea how to handle this the right way, I will show you my "high level" idea:

裹依赖供应商与其他供应商的回调:

angular.module('app', ['search','meta']).
  config(['$searchProvider','$metaProvider', function($searchProvider, $metaProvider) {
    $metaProvider.get(function(meta){
      $searchProvider.options(meta);
    });
  }]);

什么是处理这个问题的最佳方式?

What's the best way to handle this?

推荐答案

最近,我有同样的问题。我无法找到一个解决办法,所以我工作围绕这个问题由我已经加载的所有的客户端配置信息后,手动自举角框架的范围内工作。 http://docs.angularjs.org/api/angular.bootstrap

I had the same problem recently. I couldn't find a solution to work within the bounds of the framework so I worked around the problem by manually bootstrapping angular after I have loaded all of the client configuration information. http://docs.angularjs.org/api/angular.bootstrap

$.get('/api/context', function () {
    // ...
    angular.bootstrap($('#container'), ['app']);
});

这篇关于通过应用程序配置AngularJS动态提供程序选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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