Angularjs module.config中提供了哪些提供程序/服务?
我想使用$document从输入字段中获取服务器值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 +
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模块分两个阶段启动:
仅提供提供程序和常量配置阶段
根据注册的提供者实例化服务。在此阶段,常量仍然可用,但提供程序不可用运行阶段
Provider
后缀标记)。这可能回答了您的问题,但无助于解决您的模板加载问题
我想知道您是否不能在HTML中简单地使用base标记,然后简单地使用相对路径(到base),而不指定绝对路径?类似(前提是底座配置正确):
虽然这个问题已经得到了普遍的回答,但这里有一个针对问题中使用的具体示例的补充: 如何使用角度常量定义分区的baseUrl(或定义表示服务器值的其他常量并使其可用于配置): 在我看来,这有几个好处:
- 如果移动视图,只需在一个位置更新代码
- 如果您的部分深入嵌套在项目布局中,“templateBaseUrl”不会像整个路径那样引起噪音
- 您甚至可以在相对路径和绝对路径之间进行更改
- 建议使用html的base元素来消除为每个templateUrl预先添加baseUrl的需要。但这也影响了网站上的所有其他资源。仅为模板配置基本url没有副作用
// 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 );