Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stating directive templateUrl relative to root

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?

like image 955
Ufuk Hacıoğulları Avatar asked Jun 02 '13 20:06

Ufuk Hacıoğulları


4 Answers

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'
like image 161
Ufuk Hacıoğulları Avatar answered Nov 20 '22 04:11

Ufuk Hacıoğulları


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'
like image 36
Barnabas Kendall Avatar answered Nov 20 '22 04:11

Barnabas Kendall


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

it worked for me.

like image 3
Rajat Faujdar Avatar answered Nov 20 '22 02:11

Rajat Faujdar


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

like image 2
hoogw Avatar answered Nov 20 '22 03:11

hoogw