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

208
Visualizações
Angular routing based on promise - 404 error

I want to either show/hide a route based on an API call (Promise returned). If I add a conditional based on the promise value as below, the route is correctly shown/hidden correctly when I visit the URL through navigation.

However, when I hit refresh on the URL, I get a 404 error. I am completely new to Angular, JS and promises so I can't understand what exactly is going on here, or how this is resolved.

let toggle: boolean = await fetch("/pageToggle")
        .then(response => response.json())
        .then(response => parseInt(response) === 1)
        .catch(() => false);
    if (toggle) {
             $stateProvider.state('maintenance.correspondence', {
                url: '/correspondence',
                data: {
                    title: 'Correspondence',
                    header: {
                        heading: 'Correspondence',
                        subheading: 'Correspondence'
                    }
                },

                template: '<correspondence></correspondence>',
                controller: ($scope: IScope) => {
                },
                onExit: (BreadCrumbs: BreadCrumbs) => {
                    BreadCrumbs.reset();
                },
                onEnter: (BreadCrumbs: BreadCrumbs, $stateParams: IStateParamsService) => {
                    BreadCrumbs.add("Correspondence", "Correspondence", null, "maintenance.correspondence");
                }
            }
        );
    }

Another point to clarify is that I only inherited this code from another developer, and all I am trying to do is fix the issue with the redirection.

The URLs look like using HTML5 mode. The URL's are like https://localhost:3000/#/maintenance/correspondence/

Edit 4/11/2021:

I've made some progress with this. What I've observed is that the 404 only happens when I do a conditional (based on promise result) around the state. Regardless of the result of the promise boolean, Angular is not able to resolve the route.

If I remove the conditional, I don't get the 404, but I do need a way to conditionally not show the route based as needed.

I have been looking at the resolve{} method to see if I can do the API call there, but I am not sure how or where to add the conditional for the state based on the promise. So I think this is now essentially working around AngularJS state providers to do what I need, if I understand this right.

Can someone please help me implement this?

Edit - 5/11/2021 I've been trying a few things, but still stuck with this. Can someone please help?

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