简体   繁体   English

如何将特定的JSON数据从一个工厂传递到Angular JS中的另一个工厂?

[英]How to pass specific JSON data from one factory to another factory in Angular JS?

I'm trying to build a simple Weather App using Angular JS. 我正在尝试使用Angular JS构建一个简单的Weather App。

I've created a - 1)Html Page 2)3 Angular Js components - 我创建了-1)HTML第2)3个Angular Js组件-

CONTROLLER 控制器

angular
.module('weather')
.controller('weatherctrl', function($scope, factory,getGeoLoc){
    $scope.ip;
    $scope.geoloc;
//city obtainer-

        factory.getIP().success(function(data){
            $scope.ip = data;

        }).error(function(error){
            console.log("error");
        });

        //use the ip obtained on the lo/la api.

        getGeoLoc.getGeo().success(function(data){
            $scope.geoloc = data

        }).error(function(error){
            console.log("error");
        })
    });

FACTORY 1 - Gets THE IP ADDRESS 工厂1-获得IP地址

//This is the factory for data recieving.

angular
.module('weather')
.factory('factory',function($http){function getIP(){
          return $http({
    method:'GET',
    url:'http://ip-api.com/json/'
})
} return {getIP:getIP} });

FACTORY 2- This is where I face problem 工厂2- 这是我面临的问题

I've to pass the IP from the factory 1 to factory 2 to use the longitude and latitude 我必须将IP从工厂1传递到工厂2以使用经度和纬度

angular
.module('weather')
.factory('getGeoLoc',function($http){

    function getGeo(data){
        return $http({
            method:'GET',
                url:'http://api.openweathermap.org/data/2.5/weather?lat='+data.lat+'&lon='+data.lon+'&units=metric&APPID=0bd5e484b08e1c327c6bb917a530ad63',

        })


    }

    return{
        getGeo:getGeo
    }

});

I've tried passing/returning 我尝试过/返回

getGeo(getIP)

as a function parameter towards getGeo in factory 2.But it doesn't work. 作为工厂2中对getGeo的函数参数,但是它不起作用。

Please help me out, im a beginner at Angular JS. 我是Angular JS的初学者,请帮帮我。

Also the controller and both the factories are written on different js files. 控制器和两个工厂也都写在不同的js文件中。 controller.js factory1.js factory2.js controller.js factory1.js factory2.js

Both functions, factory.getIp and getGeoLoc.getGeo , return promises. 这两个函数factory.getIpgetGeoLoc.getGeo返回承诺。 To chain promises, return values or promises to the next. 链接承诺, 价值或承诺返回至下一个。

   factory.getIp()
     .then(function getIpSuccess(response){
        $scope.ip = response.data;
        //return next promise to chain
        return getGeoLoc.getGeo($scope.ip);
   }).then(function getGeoSuccess(response) {
        $scope.geoloc = response.data;
        //return data for chaining
        return $scope.geoloc;
   }).catch(function onReject(error){
        console.log("error ", error.status);
        //throw to chain error
        throw error;
   });

In the above example if the getIp operation fails, the getGeo operation will be skipped. 在上面的示例中,如果getIp操作失败,则将跳过getGeo操作。 The catch handler will get either errors from the first or second .then handler. catch处理程序将从第一个或第二个.then处理程序中获取错误。

Avoid the use of .success and .error because they ignore return (or thrown) values . 避免使用.success.error因为它们会忽略返回(或抛出)值

Instead use .then and .catch . 而是使用.then.catch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM