javascript - 将 API 调用中收集的值从第一个方法传递到同一对象中的第二个方法

标签 javascript angularjs oop parameter-passing

我有一个包含两个方法的对象。第一个是调用 API 并将响应存储在变量中。

在第二种方法中,我使用 this.nameOfFirstMethod() 执行第一个方法,然后我想根据我在第一种方法中的 API 调用中收集的数字进行一些计算。

为了更清楚地看一下代码,请开始阅读第二种方法:

this.currencyConverter = {
    getRatio: function(selectedCurrency) {
        var selectedCurrency = selectedCurrency;
        $http({
            url: 'http://api.fixer.io/latest?base='+selectedCurrency+'&symbols=PLN,CHF,EUR,USD,GBP',
            method: 'GET'
        })
        .then(function(response) {
            var currentCurrency = {
                toPLN:  response.data.rates.PLN,
                toCHF:  response.data.rates.CHF,
                toEUR:  response.data.rates.EUR,
                toUSD:  response.data.rates.USD,
                toUSD:  response.data.rates.GBP
            };
            console.log("Succesful store currentCurrency");
            return currentCurrency;
        }, function(response) {
            console.log("Problem occure while downloading money current currency!");
            console.log(response.data);
        });
    },
    convertMoney: function(selectedCurrency,priceField) {
        var priceField = priceField;
        var selectedCurrency = selectedCurrency;

        console.log('selectedCurrency in service: '+selectedCurrency);
        console.log('priceField in service: '+priceField);

        this.getRatio(selectedCurrency);

        console.log(currentCurrency);

        /*
        var converted = {
            PLN: function() { return priceField * $rootScope.currentCurrency.toPLN; },
            USD: function() { return priceField * $rootScope.currentCurrency.toUSD; },
            EUR: function() { return priceField * $rootScope.currentCurrency.toEUR; },
            CHF: function() { return priceField * $rootScope.currentCurrency.toCHF; },
            GBP: function() { return priceField * $rootScope.currentCurrency.toGBP; }
        };
        */

    }
}

如果有人不喜欢 StackOverflow 样式,这里是相同代码的 GIST: https://gist.github.com/anonymous/e03de4de1af407bf70f4038acd77c961

请打开这个要点,因为我现在将根据具体行进行解释。

所以在第 30 行我执行第一个方法。

在第 9 行中,我将检索到的数据存储在变量中,并在第 17 行中返回该数据(以便在第二种方法中使用它)。

最后,我想在第 32 行的第二个对象中console.log(目前只有 console.log 我稍后会做数学)。

它不适用于此return,第二种方法中带有console.log的行会导致以下错误:

ReferenceError: currentCurrency is not defined

最佳答案

您没有将 getRatio 的返回值分配给变量 应该是

currentCurrency = this.getRatio(selectedCurrency);

并且您应该正确使用 Promise。 所以改成这样(未测试)

this.currencyConverter = {
    getRatio: function(selectedCurrency) {
        var selectedCurrency = selectedCurrency;
        return $http({
            url: 'http://api.fixer.io/latest?base='+selectedCurrency+'&symbols=PLN,CHF,EUR,USD,GBP',
            method: 'GET'
        })
        .then(function(response) {
            var currentCurrency = {
                toPLN:  response.data.rates.PLN,
                toCHF:  response.data.rates.CHF,
                toEUR:  response.data.rates.EUR,
                toUSD:  response.data.rates.USD,
                toUSD:  response.data.rates.GBP
            };
            console.log("Succesful store currentCurrency");
            return currentCurrency;
        }, function(response) {
            console.log("Problem occure while downloading money current currency!");
            console.log(response.data);
        });
    },
    convertMoney: function(selectedCurrency,priceField) {
        var priceField = priceField;
        var selectedCurrency = selectedCurrency;

        console.log('selectedCurrency in service: '+selectedCurrency);
        console.log('priceField in service: '+priceField);

        var currentCurrency = this.getRatio(selectedCurrency);
        currentCurrency.then(res => console.log(res));

        //console.log(currentCurrency);           

    }

}

关于javascript - 将 API 调用中收集的值从第一个方法传递到同一对象中的第二个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46026736/

相关文章:

javascript - 火狐插件 : What is the best way to insert a large chunk of HTML?

c# - 日期从 .Net 到 Angularjs 的转换

javascript - 在 Ionic 和 Angular 中使用表单设置本地存储变量

java - 指标(变量、参数)及其值的 OOP 模式

没有属性样板的 Python 类

javascript - 通过 javascript 读取 web.config 值

javascript - 谷歌地图 API : Want geolocation to click automatically on data layer below

Javascript - 等待大量异步回调返回?

javascript - 是否有任何用于构建像 johnpapa/angular-styleguide 这样的 ionic 应用程序的样式指南

c++ - TypeCasting 在 C++ 中将 <int, class A> 映射到 <int, class B>