2013-06-02 42 views
57

我目前正在说明相对于当前窗口位置的templateUrl。说明相对于根的指令templateUrl

cvApp.directive('personalDetails', function() { 

    return { 
     restrict: 'A', 
     templateUrl: '../../Scripts/app/templates/personalDetails.html' 
    }; 

}); 

如何才能使templateUrl相对于应用程序的根?我正在寻找这样的事情:

templateUrl: '~/Scripts/app/templates/personalDetails.html' 

AngularJS能完成这个吗?

+0

你的网络服务器或当地的F-尝试这种ile系统?在网络服务器上,您只需使用'/ path/to/template'。 Web路径解析中没有'〜'(用户主目录)。 –

+2

我看不到OP没有明确说明上下文,但从我可以看到的是在ASP.NET Web应用程序中。所以'〜'与* web应用程序根目录* – superjos

回答

77

看起来像它的支持。从thread on AngularJS Google Group采取:

以“/”前缀的URL是相对于域,没有“/” 前缀这将是相对于主(“index.html的”)页或基本URL (如果您在html5模式下使用位置)。

这工作得很好:

templateUrl: '/Scripts/app/templates/personalDetails.html' 
+5

一旦我从templateUrl值中删除了前导斜杠,就可以在ASP.NET MVC中使用BASE标记。 – ClearCloud8

+0

这个技巧适用于我。太棒了。 –

14

看起来你正在试图做ASP.NET风格的应用程序相对路径,但客户端。此修复程序其实是HTML:添加一个基本标记在头上,就像这样:

<base href="<%= Request.ApplicationPath %>" /> 

然后保持相对于这个模板的路径,如

templateUrl: 'Scripts/app/templates/personalDetails.html' 
+0

有关。这种方法的问题是文档中的每个其他相对URL现在都有这个前缀。 –

3

使用 '././Scripts/app/templates/personalDetails.html'

它为我工作。

0

angular 4使用webpack作为打包程序。

./ means relative path to current folder 



../ means parent folder 

域= SRC

根= SRC /索引

例如app.component.ts文件下面, URL指xxx.html xxx.css是在相同的文件夹XXX。 TS

./ is relative path as current folder 

../ is relative path as parent folder 

@Component({ 
selector: 'app-root', 
templateUrl: './app.component.html', 
styleUrls: ['./app.component.css'] 
}) 

COMPONENT-RELATIVE PATHS IN ANGULAR