0

This is my initial angular function. I am setting whrong http address and working http address, but working then function in any case. If I set wrong http address it sholud be handle catch function. This is working demo.

(function () {

    loadAppSettings().then(function () {
        alert(1)
    }).catch(function(){
            alert(0)
    });

    function loadAppSettings() {
        var $http = angular.injector(["ng"]).get("$http");

        return $http.get("oops").then(
            function (response) {
                angular.module("app").constant("appSettings", response.data);
            }
        ).catch(
            function (errorResponse) {
                console.error("app settings couldn't load.")
            }
        );
    }
})();
1
  • catch function working always. If you remove catch from loadAppSettings then alert(0) will works fine. fiddle Commented Dec 18, 2015 at 9:35

2 Answers 2

2

Which version of AngularJS do you use ? Because, in my opinion, the former way of doing that was :

$http.get('/someUrl').success( function( data, status, headers, config ){
    // code in case of success
}).error( function( data, status, headers, config ){
    // code in case of error
});

(see 1.4.3 $http documentation)

And the new way (since 1.4.4) is :

$http.get('/someUrl').then( function( response ){
    // code in case of success
}, function( response ){
    // code in case of error
});

(see 1.4.4 $http documentation)

Sign up to request clarification or add additional context in comments.

3 Comments

If you check the script he has linked in the fiddle, it's 1.5.0.
Ok. So he should use the second way then.
So, @bookmarker, you should try to use the syntax described in the second part of my answer.
0

I think if you are using Angular version before 1.4.4 - for $http calls, the promise returns .success() and .error() handlers instead, which have extra parameters, instead of .then() and .catch().

Try using these function names instead.

In Angular 1.4.4 the different handlers have been deprecated.

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.