61

I am currently stating templateUrl relative to the current window location.

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

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

});

How can I make templateUrl relative to root of the application? I am looking for something like this:

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

Can AngularJS accomplish this?

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • Are you trying this on a webserver or local file system? On a webserver you would just use '/path/to/template'. There is no '~' (user home) in web path resolution. – Paul Ryan Jun 02 '13 at 21:59
  • 2
    I undestand OP did not clarify explicitly the context, but from what I can see that is within an ASP.NET web application. So the '~' relates to the idea of *web application root directory* – superjos Aug 27 '13 at 21:22

4 Answers4

84

Looks like it's supported. Taken from a thread on AngularJS Google Group:

the url with "/" prefix is relative to the domain, without the "/" prefix it will be relative to the main ("index.html") page or base url (if you use location in the html5 mode).

This works fine:

templateUrl: '/Scripts/app/templates/personalDetails.html'
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
15

Looks like you're trying to do ASP.NET style app-relative paths but client-side. The fix is actually in HTML: add a base tag in the head, like this:

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

Then keep your template paths relative to this, as in

templateUrl: 'Scripts/app/templates/personalDetails.html'
Barnabas Kendall
  • 4,317
  • 1
  • 32
  • 24
3

Use '././Scripts/app/templates/personalDetails.html'

it worked for me.

2

angular 4 use webpack as bundler.

./ means relative path to current folder



../ means parent folder

domain = src

root = src/index

for example below app.component.ts file, url means xxx.html xxx.css are in the same folder as 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

hoogw
  • 4,982
  • 1
  • 37
  • 33