Error page in AngularJS

2

Well, I'm new to AngularJS. I have a project, a single page with routes configured with a controller. Views are loaded within the element of the index.html page. Inside the controller I'm making an http call to get the data and data binding with the $ scope. As you play the module, it is applied in ng-view. So that's fine ... but I need to make an error page, in case the user accesses in the url anything that is not found, is directed to an error page .. where can I customize the error and suggest to the user to return to the main page .

    
asked by anonymous 09.05.2016 / 13:54

2 answers

4

If you are using angular-ui / ui-router, the $urlRouter provider offers the possibility of setting the fallback route during the config stage - which is used whenever the user tries to access an unconfigured route:

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

(The above example assumes that /notFound is your treatment path for page not found.)

The advantage of this method is that you can implement a Url state, thus allowing the URL originally entered 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:

    
14.09.2016 / 20:26
1

You can enter the following code in your routes. When it does not find a valid route, it redirects to route / 404

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