简体   繁体   中英

Why Javascript removed leading zeros from string?

I'm using ui-router to get the parameter value from url using $stateParam . One of the parameters is id number that may or may not begin with 00 . For example, 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. I checked the typeof($stateParams.cardholderId) which returns 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? Now, why does that change to int when I display it inside div. {{ crumb.state }} ??

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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