Reza
Reza

Reputation: 5634

$http.error promise seems to be not firing in angularjs

I am following along the sportsStore example from the Pro AngularJS book by Adam Freeman and seem to be stuck in a code example on chapter 7.

Adam uses the data.error property within the main app.html from the controller listed below, I am forcing an error by providing an invalid url, but I don't think the $scope.data.error property is getting populated:

angular.module("sportsStore")
.constant("dataUrl", "http://localhost:5500/xxx")
.controller("sportsStoreCtrl", function($scope, $http, dataUrl){

$scope.data = {};
    $http.get(dataUrl)
        .success(function(data){
           $scope.data.products = data; 
        })
        .error(function(error){
            $scope.data.error = error;
        });
});

In the app.html, the data.error field is used as a boolean

<div class="alert alert-danger" ng-show="data.error">
    Error ({{data.error.status}}). The product data was not loaded.
    <a href="/app.html" class="alert-link">Click here to try again</a>
</div>

However, the data.error field doesn't populate anything. Replacing the 'data.error' with a 'true' gives me a correct result.

Batarang shows no such error property.

enter image description here

Upvotes: 0

Views: 51

Answers (1)

Reza
Reza

Reputation: 5634

yep, it had to do with the Angular version. The bundled source code was using v1.2...which you would assume works with the code in the book, but when I pointed to the latest version CDN, the code worked as expected.

Upvotes: 2

Related Questions