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

277
Vistas
How to make a single new HttpParams() request validating if there is data return

I have a function that when the component is loaded, it returns specific data of a certain parameter (ex: /app/items) and displays them in an information balloon, but if I access a child route (ex: /app/items/ create) or (ex: /app/items/1/view) I need to do a validation to see if there is informational data in this route to display, but if not, I need to display the information from the previous parameter (ex: /app/items) . I managed to create this function below that fulfills this, but with a request inside another, which I believe is not the most appropriate. So I'm trying to refactor so that only one request is made validating these parameters. It should also be considered that if the route has numbers, it needs to be filtered and returned an array with just the strings before doing the join('/'), as you can see there in the constructor. I'm new to the area, so I'm trying to find some background to refactor this function.

public information: InformationList | any
public isLoading:boolean = true

error: any;
showError: boolean = false

public currentPageUrl = null;
public output = null
other: any;

constructor(
    public screenInfoService: ScreenInfoService, 
    public loaderService: LoaderService,
    public router: Router
) {
    this.currentPageUrl = this.router.url.split("/").filter((item: any) => {
        return isNaN(item)
    })
}

load() {
    this.output = this.currentPageUrl.join("/");
    let params = new HttpParams();
    params = params.set('orderBy', 'id');
    params = params.set('search', `path:${this.output}`); 
    params = params.set('searchFields', `path:ilike`);
    params = params.set('searchJoin', 'and');
    this.screenInfoService.getAll(params).subscribe(
        success => {
            if (success.length === 0) {
                let output2 = null
                output2 = this.currentPageUrl.slice(0,2).join("/");
                params = params.set('orderBy', 'id');
                params = params.set('search', `path:${output2}`); 
                params = params.set('searchFields', `path:ilike`);
                params = params.set('searchJoin', 'and');
                this.screenInfoService.getAll(params).subscribe(
                    success => {
                        this.information = success
                    }, error => {
                        this.error = 'MSG.T224';
                        this.showError = true;
                    }
                )
            } else {
                this.information = success
            }
        }, error => {
            this.error = 'MSG.T224';
            this.showError = true;
        });
}



ngOnInit(): void {
this.load()

}

about 4 years ago · Juan Pablo Isaza
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