我是 JavaScript 新手。
我试图了解以下代码中“outCurr”参数的值如何设置为“USD”,我是否遗漏了任何内容。我理解了其余的代码,但无法弄清楚这一点。
示例取自https://docs.angularjs.org/guide/concepts#model
angular.module('invoice1', [])
.controller('InvoiceController', function()
{
this.qty = 1;
this.cost = 2;
this.inCurr = 'EUR';
this.currencies = ['USD', 'EUR', 'CNY'];
this.usdToForeignRates = { USD: 1, EUR: 0.74, CNY: 6.09 };
this.total = function total(outCurr)
{
return this.convertCurrency(this.qty * this.cost, this.inCurr, outCurr);
};
this.convertCurrency = function convertCurrency(amount, inCurr, outCurr)
{
return amount * this.usdToForeignRates[outCurr] / this.usdToForeignRates[inCurr];
};
this.pay = function pay() {
window.alert("Thanks!");
};
});
我的理解是,这两个函数都将“outCurr”作为参数/参数,但我看不到分配给它的任何值。如果我错过了什么,请告诉我。
最佳答案
在index.html文件中,有一个ng-repeat循环遍历当前货币(['USD', 'EUR', 'CNY']数组并调用total(c)函数并传递每种货币。
<b>Total:</b>
<span ng-repeat="c in invoice.currencies">
{{invoice.total(c) | currency:c}}
</span>
因此,您还需要查看 html 文件,因为 AngularJS 绑定(bind)和表达式发生在 HTML 文件中。
希望有帮助。
关于javascript - 如何设置参数值 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824975/