Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS在返回前解决期票问题_Angularjs - Fatal编程技术网

AngularJS在返回前解决期票问题

AngularJS在返回前解决期票问题,angularjs,Angularjs,下午好 我是angularJS的新手,所以如果我问一个基本问题,很抱歉。。。 但我有一个问题: 我有一个服务Service2,它从配置中获取一个字符串,我还有另一个服务Service1,它使用这个Service2执行更多的操作,并在Service1上返回Service2+操作的URLreturn。。。 但是,在函数初始化中,它跳过 Service2.getConfig().then(function (config) { baseUrl = config.endPoint

下午好

我是angularJS的新手,所以如果我问一个基本问题,很抱歉。。。 但我有一个问题:

我有一个服务Service2,它从配置中获取一个字符串,我还有另一个服务Service1,它使用这个Service2执行更多的操作,并在Service1上返回Service2+操作的URLreturn。。。 但是,在函数初始化中,它跳过

Service2.getConfig().then(function (config) {
            baseUrl = config.endPointUrl;
        });
然后直接返回。正在获取我需要的URL。。。 您对如何让它在完成服务2的所有操作后才返回服务1有什么建议吗? 这是我要做的基本代码 服务1

服务2


您只需在获得数据后返回。getConfig似乎是一个异步调用。因此,将返回移动到.then回调是最简单的操作:

function Service1($resource, Service2) {
    var resourceUrl = "";
    var baseUrl = "";

    Service2.getConfig().then(function (config) {
        baseUrl = config.endPointUrl;
        resourceUrl = baseUrl + "/event/history/period";
        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true }
        });
    });
}

有关更多信息:,

搜索更多信息并向其他人询问此问题,我使用$http找到以下解决方案


谢谢大家!!!现在我对promise有了更多的了解:D

我尝试执行此操作,但出现了一个错误:[$injector:unde]提供程序“Service1”必须从$get factory方法返回一个值。是否可以使用async并等待?
function service2($resource, $log) {
    var configService = {};

    configService.getConfig = getConfigFromServer;

    return configService;

    function getConfigFromServer() {
        var eventDataResource = $resource('api/service-config');
        return eventDataResource.get().$promise;
    }
}
function Service1($resource, Service2) {
    var resourceUrl = "";
    var baseUrl = "";

    Service2.getConfig().then(function (config) {
        baseUrl = config.endPointUrl;
        resourceUrl = baseUrl + "/event/history/period";
        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true }
        });
    });
}
function Service1(Service2, $http, $q) {

    var eventDataService = {};

    var endPointUrl = $q.defer();
    Service2.getConfig().then(function (config) {
        endPointUrl.resolve(config.endPointUrl);
    });

    eventDataService.getCount = getCount;
    return eventDataService;

    function getEndPointUrl() {
        return endPointUrl.promise;
    }

    function getCount(successCallback, failureCallback) {
        getEndPointUrl().then(function (endPointUrl) {
            var uri = endPointUrl + '/event/gunshotCount';
            $http.get(uri).then(function (data) {
                successCallback(data);
            }, function (error) {
                failureCallback(error);
            });
        })
            .catch(function (error) {
                $log.error("Error while getting REST endpoint Url. Error : " + error);
            });
    }
}