Angularjs 如何避免角js路由器中的硬编码模板路径?
我的angular router中有以下代码片段:Angularjs 如何避免角js路由器中的硬编码模板路径?,angularjs,angular-routing,Angularjs,Angular Routing,我的angular router中有以下代码片段: staticContent = xmlDoc.getElementsByTagName("staticContent")[0].childNodes[0].nodeValue; $stateProvider .state('home', { url: "/Home", views: { "main-view": { //templateUrl:
staticContent = xmlDoc.getElementsByTagName("staticContent")[0].childNodes[0].nodeValue;
$stateProvider
.state('home', {
url: "/Home",
views: {
"main-view": {
//templateUrl: "../../../views/Home.htm", --Case 1
templateUrl: staticContent+"/Home.htm", --Case 2
controller: "homeCtrl"
}
}
})
最初,我为案例1尝试了上面的代码,效果很好。问题是,我需要避免硬编码路径,因为我的HTM文件将位于单独的服务器上
但是,当我使用案例2中的代码时,应用程序根本不会加载。
变量staticContent设置为“../../../views”。正在从XML文件读取此值(此值设置正确)。
如何避免路由器文件中的硬编码模板路径?您可以将常量插入配置块中
angular.module('app', [])
.constant('STATIC_CONTENT', xmlDoc.getElementsByTagName("staticContent")[0].childNodes[0].nodeValue)
.config(function ($stateProvider, STATIC_CONTENT) {
$stateProvider
.state('home', {
url: "/Home",
views: {
"main-view": {
templateUrl: STATIC_CONTENT + "/Home.htm",
controller: "homeCtrl"
}
}
})
});
令人惊叹的!但我很好奇,为什么之前的案例不起作用?它只是一个简单的javascript变量。。。它的值必须附加在状态参数中?