I am trying to load Html files using the following code in angular js 2. This file is available under products folder and the html file that i am trying to load is also in the same folder. Still it is not working.
import {Component} from 'angular2/core';
@Component({
templateUrl:'./products/app.component.pen.html'
})
export class PenComponent
{
}
The above file is getting loaded from the below AppComponent.
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from "angular2/router";
import {PenComponent} from "./products/app.component.pen";
import {BookComponent} from "./products/app.component.book";
@Component({
selector: 'my-app',
template: `
<header>
<ul>
<li>
<a [routerLink]="['Pen']">Pen</a></li>
<li>
<a [routerLink]="['Book']">Book</a></li>
</ul>
</header>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/pen', name: 'Pen', component: PenComponent, useAsDefault:true}
{path: '/book', name: 'Book', component: BookComponent}
])
export class AppComponent {
}
templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. AngularJS will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.
Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components. Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated for a given element in a template.
To define a template as an external file, add a templateUrl property to the @Component decorator. To define a template within the component, add a template property to the @Component decorator that contains the HTML you want to use. An Angular component requires a template defined using template or templateUrl .
Component decorator allows you to mark a class as an Angular component and provide additional metadata that determines how the component should be processed, instantiated and used at runtime. Components are the most basic building block of an UI in an Angular application.
You have to use paths relative to the root of the project for styleUrls and templateUrls (so app/foo/bar/products/app.component.pen.html
instead of ./products/app.component.pen.html
).
To use urls relative to the component file, your project must be a commonjs
module (set "module":"commonjs"
in tsconfig), and you have to include module.id
in your component decorators:
@Component({
selector: 'foo',
moduleId: module.id // <== need this
templateUrl: './foo.html'
})
Its shown here that relatives path works but i think that that are nor release yet.
You have to use full path from starting folder. For e.g. if your start folder name is app
. You have to give path as:templateUrl:app/products/app.component.pen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With