0

I am trying to create a component called my-checklist, but I'm having issues running it.

I've declared the new module in app.ts as well as imported it, but it doesn't seem to be able to find my html file.

The error I get is:

zone.js:661 Unhandled Promise rejection: Failed to load checklist.component.html ; Zone: <root> ; Task: Promise.then ; Value: Failed to load checklist.component.html undefined

https://plnkr.co/edit/WZSc1X7whm658LEHidC4?p=preview

2 Answers 2

1

The problem is in your app.ts.

You try to open your component but you link to the app your module can't find your html cause you link it to your app.ts not to your component.ts.

In your app.ts you say

bootstrap: [ App ]

It should be

bootstrap: [ MyCheckList ]

Try and follow https://angular.io/tutorial if not get it there a demos you can checkout.

Had some time made a plunker: https://plnkr.co/edit/1v0cd9y8BZ0KTQUDlhik?p=preview also note that plunker need src/*.html for some reason. normal you use ./ for that.

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

Comments

1

When trying to load checklist.component.html the URL is relative to your index.html and that is why it is failing. Just change it to

@Component({
  selector: 'my-checklist',
  templateUrl: 'src/checklist.component.html'
})

This will make it work. But, I'll recommend that you find out a way to convert your templates in a variable and avoid the relative path issue. I don't know typescript, but I know Babel do it, maybe this would work.

import template from './checklist.component.html';

@Component({
  template: template,
  selector: 'my-checklist'
})

For example, if using Babel, after transpiled the import will look like this

var template = '<p>Test</p>';

Awesome, eh?

Check this other questions, probably will help

Is it possible to import an HTML file as a string with TypeScript?
How to import external HTML files into a TypeScript class

5 Comments

Hmm, I put the src/ as you mentioned and I don't have any errors, but why isn't my template showing up on the page? After inspecting the page, I see that my component is there, but not the template in it.
Tested in the same plunker you shared.
Hmm..interesting, can you send me a copy of your plunkr? I'm still getting the same thing
Yes, I still don't see my template showing up. Shouldn't my <p>Test</p> be showing up when I include my <my-checklist> component?
Yes, it should. I miss that detail, just checked the template was loaded successfully.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.