简体   繁体   English

为什么Javascript从字符串中删除了前导零?

[英]Why Javascript removed leading zeros from string?

I'm using ui-router to get the parameter value from url using $stateParam . 我正在使用ui-router使用$stateParam从url获取参数值。 One of the parameters is id number that may or may not begin with 00 . 参数之一是ID号,它可以或可以不以00开头。 For example, 0034323343 . 例如0034323343 In this entire application, the leading zeros are intact. 在整个应用程序中,前导零保持不变。 However, there's one instance where I'm adding state to an object like this: 但是,在一个实例中,我正在向这样的对象添加状态:

SharedDataService.setBreadcrumb({
                state: 'notes({ claimID: ' + $stateParams.claimID + ', cardholderId: ' + $stateParams.cardholderId + ' })',
                name: 'Notes'
            });

For some reason, the leading zero is removed. 由于某些原因,前导零被删除。 The ID becomes 34323343 in the URL. URL中的ID变为34323343 I checked the typeof($stateParams.cardholderId) which returns string . 我检查了typeof($stateParams.cardholderId) ,它返回string So, why is zero missing? 那么,为什么零缺失呢?

Here's the object that is saved. 这是保存的对象。

Object {state: "notes({ claimID: 187337, cardholderId: 0034323343 })", name: "Notes"}

which i printed out to the console when this value is set. 设置此值时,我将其打印到控制台。 See how the state has cardholderId in correct format - with leading zeros? 看看状态如何cardholderId in正确的格式显示cardholderId-带前导零? Now, why does that change to int when I display it inside div. 现在,为什么当我在div中显示它时将其更改为int。 {{ crumb.state }} ?? {{ crumb.state }}

您必须用"括住变量,以将其作为字符串保留在字符串化对象中。

'notes({ claimID: "' + $stateParams.claimID + '", cardholderId: "' + $stateParams.cardholderId + '" })'

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

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