Refresh page with route without "#" AngularJs

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: "/" });

});
    
asked by anonymous 23.03.2018 / 14:11

2 answers

1

Error 404 is sent because of the response from your http server, which does not recognize the route, since the directory does not exist physically, nor does it pass it to the control of the angle. You must configure your server so that all traffic that generates error 404 is forwarded to your home page. In this way, the angle will take control of the request and can process through the application route definitions.

Example in nginx, with angular start page in /index.html :

location / {
  try_files $uri $uri/ /index.html;
}
    
23.03.2018 / 14:38
0

You need to determine the mapping on your server to the home 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 is translated into the server returning, for example, ~/app/index.html - and leaving the route processing 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 .

    
23.03.2018 / 15:30