Processing $http response in service

后端 未结 12 1688
半阙折子戏
半阙折子戏 2020-11-22 03:36

I recently posted a detailed description of the issue I am facing here at SO. As I couldn\'t send an actual $http request, I used timeout to simulate asynchrono

12条回答
  •  误落风尘
    2020-11-22 04:15

    Related to this I went through a similar problem, but not with get or post made by Angular but with an extension made by a 3rd party (in my case Chrome Extension).
    The problem that I faced is that the Chrome Extension won't return then() so I was unable to do it the way in the solution above but the result is still Asynchronous.
    So my solution is to create a service and to proceed to a callback

    app.service('cookieInfoService', function() {
        this.getInfo = function(callback) {
            var model = {};
            chrome.cookies.get({url:serverUrl, name:'userId'}, function (response) {
                model.response= response;
                callback(model);
            });
        };
    });
    

    Then in my controller

    app.controller("MyCtrl", function ($scope, cookieInfoService) {
        cookieInfoService.getInfo(function (info) {
            console.log(info);
        });
    });
    

    Hope this can help others getting the same issue.

提交回复
热议问题