Angularjs module.config中提供了哪些提供程序/服务?

Angularjs module.config中提供了哪些提供程序/服务?,angularjs,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 +

我想使用$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模块分两个阶段启动:

  • 配置阶段
    仅提供提供程序和常量
  • 运行阶段
    根据注册的提供者实例化服务。在此阶段,常量仍然可用,但提供程序不可用
AngularJS(部分:“模块加载和依赖项”)对此进行了深入了解:

模块是配置和运行块的集合,这些块 在引导过程中应用于应用程序。在其 最简单的形式模块由两类块的集合组成:

配置块-在提供程序注册期间执行 和配置阶段。只能注入提供程序和常量 进入配置块。这是为了防止意外实例化 在完全配置服务之前,服务的数量

运行块-获取 在创建喷油器并用于启动喷油器后执行 应用只有实例和常量可以注入到运行中 阻碍。这是为了防止在测试过程中进一步配置系统 应用程序运行时

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

我想知道您是否不能在HTML中简单地使用base标记,然后简单地使用相对路径(到base),而不指定绝对路径?类似(前提是底座配置正确):


虽然这个问题已经得到了普遍的回答,但这里有一个针对问题中使用的具体示例的补充:

如何使用角度常量定义分区的baseUrl(或定义表示服务器值的其他常量并使其可用于配置):

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

  • 如果移动视图,只需在一个位置更新代码
  • 如果您的部分深入嵌套在项目布局中,“templateBaseUrl”不会像整个路径那样引起噪音
  • 您甚至可以在相对路径和绝对路径之间进行更改
  • 建议使用html的base元素来消除为每个templateUrl预先添加baseUrl的需要。但这也影响了网站上的所有其他资源。仅为模板配置基本url没有副作用
一般来说,我不会将此解决方案与上面的硬编码值一起使用。这只是一个以最简单的方式展示该做什么的示例。要在服务器上复制应用程序,请在索引文件中定义app.js之外的值,并在服务器端生成必要的路径:

// 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>

[...]

我想我会使用相对路径的解决方案。我不想使用它是有原因的,但它可能是最好的解决方案。看看这个答案:我认为这是一个更好的解决方案。
// 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'
      })
    ;
  }]);
// 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>

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

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

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