从Angular服务中的回调传递数据

时间:2021-02-04 19:37:24

I am using AngularJS leaflet to search for a location and obtain latitude and longitude coordinates. In my service, I am calling my code to display map with within the service I wrote a callback which takes (lat,lng) and make an HTTP requests to url to get data related to those coordinates.

我正在使用AngularJS传单搜索位置并获取纬度和经度坐标。在我的服务中,我调用我的代码在服务中显示地图我写了一个回调,它接受(lat,lng)并向url发出HTTP请求以获取与这些坐标相关的数据。

Ideally, I would like to have created a function that returned an http request and returns the promise in the controller. The problem is that getMarkets is a callback and I can not call .then on the function directly because it needs to be passed the coordinates first. It is returning the data and I would like to know how to pass the data I receive from ($http(options)) to the controller so I can render response in the view.

理想情况下,我想创建一个返回http请求并在控制器中返回promise的函数。问题是getMarkets是一个回调函数,我不能直接调用函数。因为它需要首先传递坐标。它正在返回数据,我想知道如何将我收到的数据($ http(选项))传递给控制器​​,以便我可以在视图中呈现响应。

app.service('mapService', (function($http) {
    var fmCoordinates = {};

    var requestMarkets = function(lat,lng){
        var options = {
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "http://somerul?lat=" + lat + "&lng=" + lng
        };
        return console.log($http(options));

    };


L.Control.Search = L.Control.extend({
//Leaflet Code
_getLocation: function(key) {   //extract latlng from _recordsCache
        var latLong = this._recordsCache[key];
        fmCoordinates.lat = latLong.lat;
        fmCoordinates.lng = latLong.lng;
        requestMarkets(fmCoordinates.lat,fmCoordinates.lng);
        if( this._recordsCache.hasOwnProperty(key) )
            return latLong;//then after use .loc attribute
        else
            return false;
    },
})

1 个解决方案

#1


0  

https://docs.angularjs.org/api/ng/service/$q

var requestMarkets = function(lat,lng){
    var defer = $q.defer();
    var dataFromHttp = {};
    var options = {
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "http://somerul?lat=" + lat + "&lng=" + lng
    };
    $http(options).then(function(result) {
        dataFromHttp = result.data;
        defer.resolve(dataFromHttp);
    }, function(error){
        defer.reject(error);
    });
    return defer.promise;

};

Then inside of _getLocation

然后在_getLocation里面

var promise = requestMarkets(fmCoordinates.lat,fmCoordinates.lng);
promise.then(function(yourData){...}, function(error){...});

#1


0  

https://docs.angularjs.org/api/ng/service/$q

var requestMarkets = function(lat,lng){
    var defer = $q.defer();
    var dataFromHttp = {};
    var options = {
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "http://somerul?lat=" + lat + "&lng=" + lng
    };
    $http(options).then(function(result) {
        dataFromHttp = result.data;
        defer.resolve(dataFromHttp);
    }, function(error){
        defer.reject(error);
    });
    return defer.promise;

};

Then inside of _getLocation

然后在_getLocation里面

var promise = requestMarkets(fmCoordinates.lat,fmCoordinates.lng);
promise.then(function(yourData){...}, function(error){...});