Search code examples
angularjsangularjs-routing

Redirecting to error page in Angularjs


I am new to AngularJs. I have a single page app with routes configured having a controller and a view. The view get loaded inside the <ng-view></ng-view> element of the index.html page. Inside the controller I am making a http call to get the data and binding the data to the $scope. For success scenarios this works fine but if there is an error how do I plug in another view instead of the default view configured inside the angular route. PLease let me know.


Solution

  • Use $location.url() to redirect to a 404.html when error is occured

    $http.get(url,[params])
        .success(function(data, status, headers, config){
            // bind your data to scope
        })
        .error(function(data, status, headers, config) {
            $location.url('/404');
        });
    

    Then configure your $routeProvider

    $routeProvider
        .when('/404', {
            templateUrl: '404.html',
            controller: 'Four04Controller'
        })