angularjs - module.config 中有哪些提供程序/服务可用?

标签 angularjs

我想使用 $document 从输入字段中获取服务器值。

var base_url = $document[0].getElementById('BaseUrl').value;

基本 url 用于抓取模板。
var base_url = $document[0].getElementById('BaseUrl').value;

$routeProvider.when('/alert', {
  controller: function () { },
  templateUrl: base_url + '/partials/instances.html'
});

由于 $document 抛出一个未知的错误,我猜测它在配置中不可用?有没有办法找出什么可用,什么不可用?我也可以使用 $http 从服务器获取数据,但这也不可用。

最佳答案

AngularJS 模块分两个阶段进行引导:

  • Configuration phase只有提供者和常量可用。
  • Run phase其中服务基于注册的提供者进行实例化。在此阶段,常量仍然可用,但提供者不可用。

  • AngularJS documentation (部分:“模块加载和依赖项”)对此进行了深入了解:

    A module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of collection of two kinds of blocks:

    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.

    Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.



    鉴于上述情况,您只能注入(inject)常量和提供程序(在 API 文档中标有 Provider 后缀)。这可能会回答您的问题,但无助于解决您的模板加载问题......

    我想知道您是否不能简单地在 HTML 中使用 base 标记,然后简单地使用相对路径(到基础)而不指定绝对路径?有点像(前提是基础配置正确):
    $routeProvider.when('/alert', {
      controller: function () { },
      templateUrl: 'partials/instances.html'
    });
    

    关于angularjs - module.config 中有哪些提供程序/服务可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664787/

    相关文章:

    angularjs - Angular + Node + Express + Passport + oauth2orize 独特的 CORS 问题

    javascript - 将变量从 app.js 传递到 index.html

    javascript - 将过滤后的数据分配给 ng-if 中的 Controller 变量

    javascript - 出现 Angular.js 错误

    angularjs - 与 DOM 中 ng-app/ng-controller 放置相关的 Angular View 性能

    javascript - 如何返回数组中第一个元素的属性值,或者如果没有元素则返回 0?

    javascript - 显示特定对象的 'details' - 将 JSON 值从一个页面传递到另一页面以查询 API

    ajax - Selenium:如何在 AJAX 之后等待 AngularJS 完成 DOM 更新?

    angularjs - 如何访问状态参数(ui-sref)而不将它们包含在AngularJS中状态的URL属性中

    javascript - AngularJS:使用特定键以模式更新 JSON