如何在 Angular 5 组件中有条件地加载 templateUrl html 文件 [英] How to load conditionally templateUrl html file in Angular 5 component
问题描述
我的项目中有一个场景,考虑一下,我有一个 testDynamic 组件
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() { }
}
这里需要检查 override1.html 文件是否存在于 override 文件夹中,然后将此文件作为 templateUrl 加载,否则加载组件默认的 test-dynamic.html.知道如何实现这一目标.
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.?
参考下图清晰理解
推荐答案
您不能添加多个 HTML 文件.
You can't add more than one HTML file.
您可以做的是,使用 *ngIf 或 *ngSwitchCase 仅显示模板的一部分(如果这是您的意图).那么你只有一个模板html文件.
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.
然后你的模板的 html 将是这样的:
Then html of your template will be something like this:
<div *ngIf="YOUR_CONDITION">View 01</div>
<div *ngIf="YOUR_CONDITION">View 02</div>
这篇关于如何在 Angular 5 组件中有条件地加载 templateUrl html 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!