How to load conditionally templateUrl html file in Angular 5 component

不想你离开。 提交于 2019-12-13 11:36:47

问题


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


回答1:


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>



回答2:


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

Angular 2/4 component with dynamic template or templateUrl



来源:https://stackoverflow.com/questions/49974776/how-to-load-conditionally-templateurl-html-file-in-angular-5-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!