Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.JS 404 page Single Page App

Can anyone point me in the right direction on how to make a "404 - page not found" tutorial for a Single page app in angularJS.

Or even better explain how this can be achieved.

There doesn't seem to be much on the internet regarding this.

like image 414
Max Lynn Avatar asked Jul 03 '15 15:07

Max Lynn


People also ask

Is Angular good for single page applications?

AngularJS is best used in building interactive, modern, and dynamic Single Page Applications (SPA) with the help of its compelling features including two-way data binding, RESTful API handling, templates directives, deep linking, server-side communication, modularization, AJAX handling, and dependency injection.

Is Angular single page or multi page?

Angular is one of the open-source, front-end, JavaScript-based frameworks widely used in creating single-page applications on the client side.

What is single page application Angular 4?

A single page application is a website or web application that dynamically rewrites a current web page with new data from the web server, instead of the default method of a web browser loading entire new pages.


2 Answers

In an Angular SPA using the native router, if a route is not found it will hit the $routeProvider.otherwise() method thus loading a view for a route that would have typically 404'ed if delivered from a server.

angular.app('application', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {

        // If route is not configured go to 404 route
        $routeProvider.otherwise('/404');

        $routeProvider.when('404', { /* route configuration */ });

    });

The only disadvantage here is that the URL pushstate is also changed, however that would have typically happened anyway if redirected to a custom 404 by a server.

like image 92
David Barker Avatar answered Sep 28 '22 07:09

David Barker


I would not look at it as 404 page.

In your SPA (Single page app) you could make multiple API calls that independtly update widgets on a dashboard, which 9 out of 10 are successful (200's) and one fails (404), in that case you do not want to redirect users.

As David Barker said you have a otherwise that is a catch-all page.

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/login', {
        templateUrl: 'login.template.html',
        controller: 'LoginController'
      }).
      when('/', {
        templateUrl: 'dashboard.template.html',
        controller: 'DashboardController'
      }).
      otherwise({
        redirectTo: '/'
      });
  }]);

So if a user enters a incorrect route then go to the dashboard, the only problem is feedback:

1: You need a messaging service to feedback that the actual API 404 has had an error response and that can be managed using a interceptor, directive and model.

2: You can feedback a error message by using a run method and the same directive and model that look for $routeChangeError then adds a error message

I hope that helps.

like image 35
Darren Lilley Avatar answered Sep 28 '22 07:09

Darren Lilley