40

I have following app structure

app
   home
       home.html
       home.ts
       home.sass
   app.component.ts
index.html

Essentially I want to divide my pages into separate folders that have style, .ts and .html files related to them

however at the moment loading a template like this

import {Component} from 'angular2/core';

@Component({
    selector: 'admin-app',
    template: 'home/home.html'
})
export class AppComponent { }

literary loads "home/home.html" string as a template where as I want html contents of the home.html file.

1
  • 3
    template is a string. You are looking for templateUrl. Commented Jan 12, 2016 at 11:28

2 Answers 2

72

I think that if you want to pass a template, you should use

templateUrl: 'home/home.html'

That should make an http request and load the html into your component.

Sign up to request clarification or add additional context in comments.

1 Comment

I had an issue with the path too. The path had to be relative to the root folder unless you specified a module id angular.io/docs/ts/latest/cookbook/….
9

You may want to consider using Webpack. In that case, you simply:

@Component({
    selector: 'my-comp',
    template: require('./mycomp.component.html')
})

I've found it to be a solid solution thus far since it will actually throw a compile time error if the URL is incorrect.

1 Comment

This will break if you attempt to optimize the code with the Angular AOT compiler. Sadly, very little works with AOT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.