2016-07-26 55 views
-2
.filter('asRelapsedTime', function(ConfigService) { 
    var DECIMAL_STYLE = ','; 

    return function(value, decimalPlaces) { 
    let decimalizedNumber; 
    if (decimalPlaces) { 
     decimalizedNumber = value.toFixed(decimalPlaces | 0); 
    } else { 
     decimalizedNumber = value.toString(); 
    } 

    decimalizedNumber = decimalizedNumber.replace('.'. DECIMAL_STYLE); 

    return decimalizedNumber; 
    }; 
}); 

從上面的代碼中,應該可以在內部函數中使用DECIMAL_STYLE。但事實並非如此。我在這裏錯過了什麼?javascript閉包不保留來自外部範圍的變量值

+2

您是否收到錯誤? ' ''。 '呃 - 哦,是的。 – Tushar

+0

'decimalizedNumber.replace('。'。DECIMAL_STYLE);':這裏有一個錯字。此代碼無法執行。 –

+0

令人難以置信..需要在我的編輯器@DenysSéguret中增加字體大小..將您的評論作爲答案,以便我可以將其標記 – runtimeZero

回答

1

decimalizedNumber.replace('.'. DECIMAL_STYLE);:這裏有一個錯字。

此代碼無法執行。

使用逗號將DECIMAL_STYLE作爲第二個參數傳遞給replace()

decimalizedNumber.replace('.', DECIMAL_STYLE); 
          ^

如果字符串中有多個.,使用正則表達式與g標誌全部更換OCCURENCES

decimalizedNumber.replace(/\./g, DECIMAL_STYLE);