我在网站的每个页面上都有一个组件,该组件根据页面显示一些不同的信息。这对于我所有的固定URL都很好,因为一个简单的ngIf会照顾每个页面。但是,根据事件的ID数量,我还有无限的页面可能。有没有一种方法可以检查ngIf(最多一个特定的URL),然后应用一些通用的“如果URL的这部分后面有内容,请执行此操作”?
这是我在固定页面上正在做的事情:
<div id="routing-wrapper" *ngIf="router.url == '/events'">
<hr id="line">
<span class="gray-text">Home / </span>
<span class="gold-text">Events</span
</div>
以下是我的问题的一个示例:
<div id="routing-wrapper" *ngIf="router.url == '/events/263'">
<hr id="line">
<span class="gray-text">Home / Events / </span>
<span class="gold-text">it works</span>
</div>
我在'/ events /#'之后可以有任何数字,因此我显然无法对ngIf中的每个数字进行硬编码。
答案 0 :(得分:0)
退房位置服务可以执行以下操作: Angular location service
尝试:
<div id="routing-wrapper" *ngIf="location.path().match(/^\/events\/(\d+)/)">
<hr id="line">
<span class="gray-text">Home / Events / </span>
<span class="gold-text">it works</span>
</div>
不要忘记在构造函数中添加位置:
constructor(private location: Location) { }
答案 1 :(得分:0)
这真是比我预期的简单得多的解决方案。
<div id="routing-wrapper" *ngIf="router.url.startsWith('/events/')">
<hr id="line">
<span class="gray-text">Home / Events / </span>
<span class="gold-text">it works</span>
</div>