1

What I am doing

  • I am trying to create a service which loads the user budgets for current month. The code looks like

    $scope.thisMonthBudgetSummary = function () { console.log('retrieving budget summary');

        var date = new Date();
        BudgetSummary.get({'month': date.getMonth() + 1, 'year': date.getFullYear()},
            function () {
                // success
            },
            function (error) {
                //error
                console.log('error:', error.status);
            })
    };
    

Problem
- I want to render a different view on Single Page when HTTP response is 404, what is the recommendation on that? I am confused
- I believe its not a good idea to hard code the template name in Controller

1
  • I think you should redirect to a 404 route if you don't want to hard code the template name Commented Apr 19, 2014 at 16:41

2 Answers 2

1

An $http interceptor is certainly one way, in your application configuration you could do:

$httpProvider.interceptors.push(['$q', '$location', function ($q, $location) {
                return {
                    'request': function (config) {
                        return config || $q.when(config);
                    },
                    'requestError': function (rejection) {
                        return $q.reject(rejection);
                    },
                    'response': function (response) {
                        return response || $q.when(response);
                    },
                    'responseError': function (rejection) {
                        if (rejection.status == "404") {
                           $location.path("/yourErrorRoute")
                        }
                        return $q.reject(rejection);
                    }
                };
}])
Sign up to request clarification or add additional context in comments.

Comments

1

If you are wanting a default functionality across your app, @Mohammad Sepahvand's answer works. If you simply want a one-off for that request, you can do:

var date = new Date();
BudgetSummary.get({'month': date.getMonth() + 1, 'year': date.getFullYear()},
    function (data, status, headers, config)  {
        //check your status here-->
        if (status === '200'){
            $location.path('/path/to/other/route');
        }
    },
    function (data, status, headers, config){
        //check your status here-->
        if (status === '404'){
            $location.path('/path/to/other/route');
        }
    })
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.