6

How do I add a html template to a directive? Here is a sample code which does not work :

import { Directive } from '@angular/core';

@Directive({
selector: '[master-side-bar]',
templateUrl : 'master-side-bar.html' })

export class MasterSideBar {

constructor() {

console.log('Hello MasterSideBar Directive'); 

}

}

I get the following error :

Argument of type '{ selector: string; templateUrl: string; }' is not assignable to parameter of type 'Directive'. Object literal may only specify known properties, and 'templateUrl' does not exist in type 'Directive'.

2 Answers 2

7

From your code, you need to use component not directives.

Component is the only directive which accepts template as HTML.

Please check Types of directives

Hope it helps!!

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

1 Comment

Thanks for pointing out component. I changed to use component instead and it worked.
3

According to Angular docs there are 2 types of directives: attribute and structural. None of them support templates. Just use angular Component. Is there some good reason that you want to use directive necessarily?

1 Comment

Thanks for pointing out component. I am pretty new to Angular 2. I changed to use component instead and it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.