Use templateUrl from an external library in Angular

67 views Asked by At

In my angular 15 app I have many components that use the same template, by referencing to the same template file in TemplateUrl.

for example:

@Component({
  selector: 'app-my-comp',
  templateUrl:'../../myTemplate.html',// this is a common template,
})
export class MyComponent {
}

For some reason I need to take  the  myTemplate.html of the app, and set it in an external library in my project.

  1. How can I export a HTML template from a library (in a library's public API I can export only ts files)?

  2. How can I use it in the TemplateUrl of MyComponent component?

1

There are 1 answers

2
Zerotwelve On

maybe I am wrong but I don't think this is possible. Also using the same Template in multiple components is something to use with caution.

Maybe you should explain your use-case and then we can look for different solutions/architectures.

For example if you need the same template but different functionality, you should move the UI in a so-called "dumb component" and re-use it in multiple smart components.