Update page with route without "#" Angularjs

Asked

Viewed 295 times

1

I changed the routes to load the pages without the "#".

The problem is when I do F5 it returns error 404.

var app = angular.module("app", ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {

    $locationProvider.html5Mode(true);
    $routeProvider
    .when('/Cadastros', { templateUrl: $("#linkBase").attr("href") + 'templates/Cadastro/cadastros.html', controller: 'CadastroController' })

    .otherwise({ redirectTo: "/" });

});

2 answers

1

Error 404 is sent because of the answer from your http server, which does not recognize the route, since the directory does not physically exist, nor does it pass to the angular control. You need to configure your server so that all traffic that generates error 404 is redirected to your home page at the angle. This way, the angular will take control of the request and can process by the application’s route definitions.

Example in Nginx, with angular homepage in /index.html:

location / {
  try_files $uri $uri/ /index.html;
}

0

You need to determine the mapping, on your server, to the base page.

When the user accesses the following URL:

https://www.meuservidor.com/app/#cadastros/123

In fact, the content accessed is:

https://www.meuservidor.com/app/
               |                #cadastros/123
               |                      |
           Página-base           estado/rota

What translates to the server returning, for example, ~/app/index.html - and leaving the route treatment to the Angular.

However, if you access:

https://www.meuservidor.com/app/cadastros/123

You will probably receive a 404. This is because the URL containing the route parameters is a valid address.

You then need to intercept, on the server, the URL provided by the user and redirect to your base page as in this example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.