Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda