0

I have a controller and service.

The service performs a http.get and return true if successful and false if error.

However, in the controller, it receives true or false correctly, but the html of the binding always displays as true??

Controller:

app.controller('MyController', function($scope, MyService) {

    MyService.getData(function(isLoggedIn, userData) {

        var loggedIn = isLoggedIn;    
        $scope.isLoggedIn = loggedIn;
        $scope.myUser = userData;
        //$scope.$evalAsync();
        //$scope.$apply();            
    });    
});

app.factory('MyService', function($http, $q) {
    return {

        getData: function(isLoggedIn, userModel) {
            $http.get('../assets/data/data.json')
                .success(function(data) {
                    userModel(data);
                    isLoggedIn(true);
                })
                .error(function(data, status, headers, config) {
                    // If 400 or 404 are returned, the user is not signed in.
                    if (status == 400 || status == 401 || status == 404) {
                        isLoggedIn(false);
                    }                    
                });
        }
    }
});

HTML:

{{isLoggedIn}}

In the above, {{isLoggedIn}} is always true. Even when i modify the http call to:

$http.get('../blah/blah/blah.json')

In the effort to force a .error/fail.

I've tried $scope.$apply() and i keep getting the error of a digest cycle in process. help!!!

4
  • 2
    Your using isLoggedIn and userModel in the service like they're methods... what are you passing into the service? Commented Feb 3, 2016 at 14:58
  • @tymeJV - im passing nothing to the service.. Commented Feb 3, 2016 at 14:59
  • 2
    getData is defined with two parameters, you are passing one argument (the anonymous function). And userModel in the $http.get callback is going to be undefined as you didnt pass a second argument so userModel(data); should be throwing an error, did you check your console? Commented Feb 3, 2016 at 15:03
  • @PatrickEvans - correct, userModel is throwing an error Commented Feb 3, 2016 at 15:03

1 Answer 1

1

I think you're a bit confused as to how the callbacks are working. Your isLoggedIn param in the service is the callback function that you're passing in. Your code corrected:

app.controller('MyController', function($scope, MyService) {

    /*
    The function(response) { is your callback function being passed to the service
    */
    MyService.getData(function(response) {
        var loggedIn = response.isLoggedIn;    
        $scope.isLoggedIn = loggedIn;
        $scope.myUser = response.userModel;           
    });    
});

app.factory('MyService', function($http, $q) {
    return {
        getData: function(callback) {
            $http.get('../assets/data/data.json')
                .success(function(data) {
                    //Execute your callback function and pass what data you need
                    callback({userModel: data, isLoggedIn: true});
                })
                .error(function(data, status, headers, config) {
                    // If 400 or 404 are returned, the user is not signed in.
                    if (status == 400 || status == 401 || status == 404) {
                        callback({isLoggedIn: false});
                    }                    
                });
        }
    }
});

You should be using promises... here's an even more refactored version:

app.controller('MyController', function($scope, MyService) {
    MyService.getData().then(function(response) {
        var loggedIn = response.isLoggedIn;    
        $scope.isLoggedIn = loggedIn;
        $scope.myUser = response.userModel;           
    });    
});

app.factory('MyService', function($http, $q) {
    return {
        getData: function() {
            return $http.get('../assets/data/data.json')
                .then(function(response) {
                    return {
                        userModel: response.data, 
                        isLoggedIn: true
                    };
                }, function(data, status, headers, config) {
                    // If 400 or 404 are returned, the user is not signed in.
                    if (status == 400 || status == 401 || status == 404) {
                        return {isLoggedIn: false};
                    }                    
                });
        }
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks that makes sense, there are 3 levels of return in the refactored version, are all necessary? Just trying to understand it a bit more..
Yeah... so the first return is just your object with all your calls as functions. The second return returns the entire $http promise, and the 3rd returns the data from the $http call to be accessible via .then @OamPsy
Thanks for the explanation, {{isLoggedIn}} is still displaying as true in the html when a console.log of it is false
Where are you logging it? Is the service returning false?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.