我想使用 $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/