Error page in Angularjs

Asked

Viewed 615 times

1

Well, I’m new to Angularjs. I have a project, a single page with routes configured with a controller. Views are loaded into the element of the index.html page. Inside the controller I am making an http call to get the data and data link with the $ scope. As I play the module, it is applied in ng-view. So far so good.. but I need to make an error page, if the user accesses the url anything that is not found, is directed to an error page.. where I can customize the error and suggest to the user to return to the main page.

  • You’re referring to treat error 404?

  • That’s when someone types something in the url that doesn’t exist.. or there is an error be in registration or something pertinent, to have a customized view of the error..

  • Who handles the request is your server, whether ASP or PHP or any other. For example in PHP it is done using the httpd. The angler won’t be able to do that unless of course you want to treat if the return of ng-route empty shaft.

2 answers

4


If you are using angular-ui/ui-router, the provider $urlRouter offers the possibility of configuring the fallback during the stage config - that is used whenever the user tries to access a route not configured:

app.config(
    function ($urlRouterProvider) {
        $urlRouterProvider.otherwise("/notFound");
    });

(The above example assumes that /notFound is your route from treatment to page not found.)

The advantage of this method is that you can implement a state without Url, thus allowing the URL originally informed by the user to be kept in the navigation bar. Example:

    $stateProvider.
        state("notFound", {
            templateUrl: "statusNotFound.html",
            controller:
                function ($scope, $http) {
                        // Seu código aqui
                    });
                }
        });

Example:

inserir a descrição da imagem aqui

1

You can enter the following code on your routes. When you cannot find a valid route, it redirects to the /404 route

$routeProvider
.when('/home', {template: homeTemplate})
.otherwise({
    redirectTo: '/404'
});

Browser other questions tagged

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