You can achieve the desired effect without changing the templateUrl
all over your code base by using UrlResolver
, available since 2.0.0-alpha.36.
import {UrlResolver} from '@angular/compiler';
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
let MyApp: any;
@NgModule({
imports: [BrowserModule],
providers: [{provide: UrlResolver, useValue: new UrlResolver("http://localhost/app")}],
bootstrap: [MyApp]
})
class AppModule {
}
export function main() {
platformBrowserDynamic().bootstrapModule(AppModule);
}
This will bootstrap your application with a custom instance of UrlResolver
that has a base url that you specify. You can even have finer control over resolving the paths by extending the UrlResolver class:
class MyUrlResolver extends UrlResolver {
resolve(baseUrl: string, url: string): string {
// Serve CSS files from a special CDN.
if (url.substr(-4) === '.css') {
return super.resolve('http://cdn.myapp.com/css/', url);
}
return super.resolve(baseUrl, url);
}
}
@NgModule({
imports: [BrowserModule],
providers: [{provide: UrlResolver, useClass: MyUrlResolver}],
bootstrap: [MyApp]
})
...
(source)
Note: for this to work, you most likely need to use component-relative paths by providing moduleId: module.id,
in your component definition, e.g.:
@Component({
moduleId: module.id, // use component-relative paths
selector: 'dialogs',
templateUrl: './dialogs.html',
})