2014-11-09 58 views
1

我是javascript新手。參數值如何設置 - 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」作爲參數/參數,但我不能看到分配給它的任何值。請讓我知道我是否遺漏了任何東西。

回答

1

在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結合和表達發生在HTML文件中。

希望有所幫助。

+0

非常感謝你:) – Accountant 2014-11-09 04:56:44

+0

不用擔心,如果這個回答您的問題請標明的問題爲已回答了其他開發人員的利益,謝謝。 – 2014-11-09 07:23:09

0

變量「outCurr」被分配自動調用該函數後,在這裏看到的代碼片段中,函數會轉換tCurr永遠不會在Javascript中調用。關於AngularJS的特殊之處在於它能夠在HTML本身中調用這些函數。您會注意到,正如Omar所解釋的,在下面的代碼片段中,它將傳入「currency」數組中每種貨幣的outCurr,從而顯示每種貨幣的結果值。

<span ng-repeat="c in invoice.currencies"> 
    {{invoice.total(c) | currency:c}} 
</span>