Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
How do I hide an Agular component on all routes that correspond to a certain pattern?

I am working on an e-commerce app who's front-end is made in Angular 13.

The UI has a sidebar which I do not want to display on the product details page.

For this purpose, in app\app.component.ts I have:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'E-commerce';
  constructor(private router: Router) {}

  ngOnInit() {}

  /**
   * Check if the router url contains the specified route
   *
   * @param {string} route
   * @returns
   * @memberof AppComponent
   */
   hasRoute(route: string) {
    return this.router.url.includes(route);
  }
}

In app\app.component.html:

<div class="app-wrapper">
  <app-navbar></app-navbar>
    <div class="container">
      <div class="row my-3">
        <div *ngIf="!hasRoute('products/show/:id')" class="col-sm-6 col-md-4 col-lg-3">
          <app-sidebar class="app-sidebar"></app-sidebar>
        </div> 
        
        <div class="col-sm-6 col-md-8 col-lg-9">
          <router-outlet></router-outlet>
        </div>
      </div>
    </div>
  <app-footer class="app-footer"></app-footer>
</div>

The problem

For a reason I have been unable to understand, this solution fails, and the sidebar is displayed anywhere on the app.

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

<div *ngIf="notProductDetails()" class="col-sm-6 col-md-4 col-lg-3">
   <app-sidebar class="app-sidebar"></app-sidebar>
</div> 

HTML ^^^

constructor() {}
public notProductDetails(): void {
    return !window.location.pathname.startsWith('/products/show/');
}

TS

Simply use the window location to pull the pathname instead of injecting the router - remove the constructor injection. Also no need to pass in a prop value there, because you only have a single string you are asserting.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In that way, you are running that value just one time. In order to achieve this you can subscribe to the router events like this:

public showBar: boolean = true;

constructor(private readonly router: Router) {
   this.router.events
      .pipe(filter((event) => event instanceof NavigationEnd))
      .subscribe(({ urlAfterRedirects }: NavigationEnd) =>
        this.showBar = this.hasRoute(urlAfterRedirects)
      );
}
<div *ngIf="showBar" class="col-sm-6 col-md-4 col-lg-3">
   <app-sidebar class="app-sidebar"></app-sidebar>
</div> 

In this way, you are updating the showBar value every time the navigation end.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In case someone may find it useful, here is the final solutions I have applied to this problem:

In app\app.component.ts:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title: string = 'E-commerce';
  constructor(private router: Router) {}

  ngOnInit() {}

  routeIncludesNot(route: string) {
    return !window.location.pathname.startsWith(route);
  }
}

In app\app.component.html I have:

<div class="app-wrapper">
 <app-navbar></app-navbar>
  <div class="container">
    <div class="row my-3">
      <div *ngIf="routeIncludesNot('/products/show/')" class="col-sm-6 col-md-4 col-lg-3">
        <app-sidebar class="app-sidebar"></app-sidebar>
      </div> 
      
      <div [ngClass]="routeIncludesNot('/products/show/') ? 'col-sm-6 col-md-8 col-lg-9' : ''">
        <router-outlet></router-outlet>
      </div>
    </div>
  </div>
 <app-footer class="app-footer"></app-footer>
</div>

See a "live demo" HERE.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda