【问题标题】:AngularJS : Factories update variablesAngularJS:工厂更新变量
【发布时间】:2015-03-12 21:45:04
【问题描述】:

承诺解决后,我需要从我的工厂更新变量notifications。 我们的想法是这样称呼它:

InboxNotificationFactory.notifications

这将返回 0,然后当 promise 被解决时,它将更新为其他值,而无需在我的控制器中使用 .then()。

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

    return {
        notifications: 0
    };

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

【问题讨论】:

    标签: angularjs promise factory


    【解决方案1】:

    this.notifications 不存在。像这样的东西可能更接近你想要的:

    inboxApp.factory('InboxNotificationFactory', function (inboxHttpService) {
        var notifications = 0;
    
        inboxHttpService.totalUnreadMessages().then(function(response) {
            notifications = response.data;
        });
    
        return {
            notifications: notifications
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2015-09-19
      • 1970-01-01
      • 2015-12-26
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多