Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Component different templates

I have a component "course". I use this component to a list. This list sometimes is horizontal and some times is vertical. Can I choose dynamicaly inside the component the template file?

@Component({
    selector: 'course',
    templateUrl: getTemplateFile()
})

Something like that would be great feature!

like image 783
Michalis Avatar asked May 26 '17 07:05

Michalis


People also ask

Can a component have multiple templates?

Note Although it's possible for a component to render multiple templates, we recommend using an if:true|false directive to render nested templates conditionally instead. Create multiple HTML files in the component bundle.

What are the different templates in Angular?

There are two types of templates: Static Template. Dynamic Templates.

Can we use both template and templateUrl in Angular?

Angular 2 allows to write multi-line templates by using ` characters to enquote them. It is also possible to put multi-line template into . html file and reference it by templateUrl .

What is component and template in Angular?

A component controls a patch of screen called a view. It consists of a TypeScript class, an HTML template, and a CSS style sheet. The TypeScript class defines the interaction of the HTML template and the rendered DOM structure, while the style sheet describes its appearance.


1 Answers

I think that this tutorial is very helpful

https://scotch.io/tutorials/component-inheritance-in-angular-2

You can simply extend your base component and overwrite the template. This allows you to have different components with the exact same functionality, but different templates.

like image 147
Michalis Avatar answered Sep 30 '22 16:09

Michalis