AngularJS promises not firing when returned from a service [duplicate]

半腔热情 提交于 2019-11-28 07:03:07

You need to call your callback with $apply since it's called outside of Angular. Since this is a service, you'll need to include $rootScope in your service:

beta.service('betaServ', function($q, $rootScope) {
    this.slow = function () {
        d = $q.defer()
        setTimeout(function () {
            console.log('before resolve');
            $rootScope.$apply(d.resolve);
            console.log('after resolve');
        }, 2000);
        return d.promise;
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!