9

There is one scenario in my project, Consider, I have one testDynamic component

@Component({
    templateUrl: "./test-dynamic.html", // Need to override this file
    styleUrls: ['./test-dynamic.css']
})
export class testDynamic {
    constructor() { }
}

here need to check if an override1.html file is exists in override folder then load this file as templateUrl otherwise load the component default test-dynamic.html. Any idea how to achieve this.?

refer the following image for clearly understanding

enter image description here

ranjit redekar
  • 919
  • 1
  • 12
  • 24
  • 2
    You will check the below:
    https://stackoverflow.com/questions/31692416/dynamic-template-urls-in-angular-2%5B/link%5D
    – GSK Apr 23 '18 at 06:48
  • 1
    @GSK Thanks, I read you shared link and tried on localhost but almost all solutions are not working with Angular 5. – ranjit redekar Apr 23 '18 at 10:04
  • Refer this link https://stackoverflow.com/questions/45376628/angular-2-4-component-with-dynamic-template-or-templateurl – ranjit redekar May 03 '18 at 09:00

2 Answers2

3

refer the following link, this is a good example of dynamic templateUrl

Angular 2/4 component with dynamic template or templateUrl

ranjit redekar
  • 919
  • 1
  • 12
  • 24
2

You can't add more than one HTML file.

What you can do is, use *ngIf or *ngSwitchCase to show only parts of the template if that is your intention. Then you have only one template html file.

Then html of your template will be something like this:

<div *ngIf="YOUR_CONDITION">View 01</div>
<div *ngIf="YOUR_CONDITION">View 02</div>
Rukshan Dangalla
  • 2,500
  • 2
  • 24
  • 29
  • Thanks for your answer, but it does not work for me because we are building a framework on Angular and this is one scenario to provide template overriding facility to users. One question is it possible before AOT? – ranjit redekar Apr 30 '18 at 08:39