У меня есть следующая директива, я хочу скрыть / показать элементы на основе текущей страницы. Директива работает только при первой загрузке страницы. при переходе на другую страницу по ссылке не работает должным образом. Так что проблема в том, что директива не обновляется событием url.
Обратите внимание, что эта директива в настоящее время присоединена к headerComponent, который всегда видим и не изменяется при переходе на другие страницы
@Directive(
{ selector: '[ngIfPage]', })
export class NgIfCurrentPageDirective {
private currentPath: string;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private router: Router) {
this.currentPath = this.router.url;
this.router.events.subscribe(() => {
if (event instanceof NavigationEnd) {
this.currentPath = this.router.url;
}
});
}
@Input('ngIfPage') set pageName(option) {
if (this.shouldDisplayElement(option)) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
shouldDisplayElement(option) {
switch (option) {
case Pages.WorkflowList: {
return this.testRegxPattern('\/some-page\/?$');
}
case Pages.Workflow: {
return this.testRegxPattern('\/some-page\/.+');
}
default: {
return false;
}
}
}
testRegxPattern(pattern: string) {
const regExp = new RegExp(pattern);
return regExp.test(this.currentPath);
}
}