2012-09-30 128 views
12

我想用$文件抓住从输入字段的服务器值。module.config中提供了哪些提供者/服务?

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' 
}); 

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

+0

看看这个答案︰http://stackoverflow.com/questions/17011616/using-a-relative-path-for-a-service-call-in-angularjs/17112017#17112017我认为这是一个更好的解决方案。 –

回答

16

AngularJS模块自举在2个阶段:

  • Configuration phase,其中仅提供者和常数可用。
  • Run phase其中服务是基于注册的供应商实例化。在这个阶段,常量仍然可用,但不是提供者。

的AngularJS documentation(部分:“加载模块&相关性”)给出了洞察此:

模块是配置的集合和运行的过程中得到 施加到应用程序块引导过程。在其最简单的 形成模块是由两种块的集合:

配置块 - 在供应商登记 和配置阶段得到执行。只有提供者和常量可以注入 到配置块中。这是为了防止服务意外实例 他们已经完全配置之前。

运行块 - 创建喷油器后得到 执行和使用的kickstart的 应用。只有实例和常量可注入运行 块。这是为了防止在 应用程序运行时进一步系统配置。

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

我不知道,如果你不能简单地使用基本标记在HTML,然后简单地使用相对路径(该基地),而不指定绝对路径?STH等(条件是所述基正确配置):

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

我想我会走相对路径的解决方案。有我不想使用的原因,但它可能是最好的解决方案。 – Pickels

16

虽然问题通常被回答,这里有一个小加成靶向在问题中使用的具体的例子:

如何使用的角度恒定定义你的泛音的的baseUrl(或定义表示服务器值,使它们可用于配置其它常量):

// file: app.js 
'use strict'; 

/* App Module */ 
angular.module('myApp', []) 

    // define the templateBaseUrl 
    .constant('templateBaseUrl', 'themes/angular/partials/'); 

    // use it in configuration 
    .config(['$routeProvider','templateBaseUrl', function($routeProvider,templateBaseUrl) { 
    $routeProvider 
     .when('/posts', { 
     templateUrl : templateBaseUrl + 'post-list.html', 
     controller : PostListCtrl 
     }) 
     .when('/posts/:postId-:slug', { 
     templateUrl : templateBaseUrl + 'post-view.html', 
     controller : PostViewCtrl 
     }) 
     .when('/about', { 
     templateUrl : templateBaseUrl + 'about.html', 
     controller : AboutPageCtrl 
     }) 
     .when('/contact', { 
     templateUrl : templateBaseUrl + 'contact.html', 
     controller : ContactPageCtrl 
     }) 
     .otherwise({ 
     redirectTo: '/posts' 
     }) 
    ; 
    }]); 

在我看来,这有几个好处:

  • 如果你移动你的意见,你只需要在一个单一的位置更新代码
  • 如果你的谐音被深度嵌套项目布局中,“templateBaseUrl”不会造成尽可能大的噪音整个路径
  • 你甚至可以在相对路径和绝对路径之间切换
  • An answer to a similar question建议使用html的基本元素来删除对每个templateUrl预先添加baseUrl的需要。但是这也影响了网站上的所有其他资源。仅配置模板的基本url没有副作用

通常,我不使用此解决方案,并使用上面的硬编码值。这只是一个例子,以最简单的方式展示要做什么。为了能够复制左右你的应用服务器上,定义值app.js之外,在你的索引文件并生成必要的pathes服务器端:

// file: index.php 
<?php 
    // only depends on your project layout 
    $angularPartialsBaseUrl = 'themes/angular/partials/'; 
    // this will change when you move the app around on the server 
    $themeBaseUrl = $_SERVER['REQUEST_URI'] . 'themes/angular'; 
?><!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
    <title>Yii Blog Demo</title> 

    <script> 
     var myNS = myNS || {}; 
     myNS.appConfig = myNS.appConfig || {}; 
     myNS.appConfig.templateBaseUrl = '<?php echo $angularPartialsBaseUrl; ?>'; 
    </script> 

    <script src="<?php echo $themeBaseUrl; ?>/js/vendor/angular/angular.js"></script> 
    <script src="<?php echo $themeBaseUrl; ?>/js/app.js"></script> 

</head> 

[...] 

而且在app.js:

// file: app.js 
'use strict'; 

/* App Module */ 
angular.module('myApp', []) 

    // define the templateBaseUrl using external appConfig 
    .constant('templateBaseUrl', myNS.appConfig.templateBaseUrl);