James
James

Reputation: 1392

Angular is finally not firing in controller

I'm using a angular factory to run inside my controller, however my finally doesn't fun in the controller, it does run in the factory below is the code: factory -

var createProfile = function (profile) {
    var deferred = $q.defer();

    $http.post("localhost/profile", profile)
        .success(function(data, status){
            deferred.resolve(data);
        })
        .error(function(error, status){
            $rootScope.error = sitesettings.parseErrors(error);              
        })
        .finally(function(){
            console.log('hello'); // **this message logs**
        });

    return deferred.promise;
};

and in my Controller I have this:

profileFactory.createProfile (profile)
    .then(function (data) {
        // **works if successful**

    })
    .finally(function () {
         console.log('fin'); // **this never fires, successfully or on an error**
    });

I guess I could pass my object into the profileFactory like profileFactory.createProfile (profile, myObject) and return it, but it seems counter intuitive.

Can somebody please advise. thank you.

kind regards

Upvotes: 1

Views: 1061

Answers (1)

tandrewnichols
tandrewnichols

Reputation: 3466

It's because you're returning a different promise, which you never manually resolve. If you just return $http.post(//etc.) it should work fine.

EDIT:

I might've spoken to soon. I missed that you were resolving in your success. But that seems unnecessary. Just do

return $http.post("localhost/profile", profile);

and have your controller attach success, error, and finally handles.

Upvotes: 2

Related Questions