最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load conditionally templateUrl html file in Angular 5 component - Stack Overflow

programmeradmin1浏览0评论

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

@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 ponent default test-dynamic.html. Any idea how to achieve this.?

refer the following image for clearly understanding

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

@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 ponent default test-dynamic.html. Any idea how to achieve this.?

refer the following image for clearly understanding

Share Improve this question asked Apr 23, 2018 at 6:35 ranjit redekarranjit redekar 9471 gold badge12 silver badges25 bronze badges 3
  • 2 You will check the below: <br> stackoverflow./questions/31692416/… – GSK Commented Apr 23, 2018 at 6: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 Commented Apr 23, 2018 at 10:04
  • Refer this link stackoverflow./questions/45376628/… – ranjit redekar Commented May 3, 2018 at 9:00
Add a ment  | 

1 Answer 1

Reset to default 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>
发布评论

评论列表(0)

  1. 暂无评论