0

I need to update the variable notifications from my factory after the promise is resolved. The idea is to to call it like this:

InboxNotificationFactory.notifications

and this will return 0, and then when the promise is resolved it will update to some other value without the need to used .then() inside my controller.

inboxApp.factory('InboxNotificationFactory', function (inboxHttpService) {
    getNotifications();

    return {
        notifications: 0
    };

    function getNotifications() {
        inboxHttpService.totalUnreadMessages().then(function(response) {
            //ERROR RIGHT HERE
            this.notifications = response.data;
        });
    }
});

1 Answer 1

1

this.notifications doesn't exist. Something like this is probably closer to what you want:

inboxApp.factory('InboxNotificationFactory', function (inboxHttpService) {
    var notifications = 0;

    inboxHttpService.totalUnreadMessages().then(function(response) {
        notifications = response.data;
    });

    return {
        notifications: notifications
    };
});
Sign up to request clarification or add additional context in comments.

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.