I am facing an issue of converting string value to numbers when i pass into my input field. I have used parseInt but instead getting an NAN value .

Here is my input field in html:

<div class="row">
 <div class="col-md-12">
 <div class="form-group col-md-6">
 <label>Form id:</label>
 <input type= "text" name="fromId" class="form-control" ng-model="IssueReceipt.formId" ng-required="true" readonly> <span class="error" ng-show="issueReceiptDataEntryform.fromId.$dirty && issueReceiptDataEntryform.fromId.$error.required "></span> 
                            </div>

And my script page where i pass the value :

$scope.getRandomForm = function() {
          paymentService.NewEReceipt().success(function(data){
                console.log(data)
                $scope.IssueReceipt.formId = parseInt(data);
                console.log(parseInt(data));
             })

        }
        $scope.getRandomForm()

Let me show u the console i'm getting :

enter image description here

If i use parseInt, i'm getting NAN as the value instead of number. Please can anyone help me with this ?

share|improve this question
    
Try triming the data before parsing to int. I dont see something wrong with the code as well as the value. – Jaganathan Bantheswaran 1 hour ago
    
@Jaganathan Bantheswaran The value i'm passing is an API value . The data in the controller i'm passing consists of values in it. – anil chean 47 mins ago
    
@Jaganathan Bantheswaran I'm getting the values . but, it is in the form of string .but, i need it in numbers. – anil chean 46 mins ago
    
I aware that data is string. The reason i told you to trim the string data is to avoid the space if any in the string. Try like this parseInt(data.trim()) – Jaganathan Bantheswaran 42 mins ago
    
@Jaganathan Bantheswaran i tried but still getting as NAN value . – anil chean 38 mins ago

Its working fine for me. I have hard coded the value in controller instead of data. You can send ur entire code so that I can check or just trim the data or debug and check it might be a simple issue

$scope.getRandomForm = function() {

      var data = "12345";
      $scope.IssueReceipt.formId = parseInt("12345");
       console.log(parseInt(data));


    }
    $scope.getRandomForm();

i am getting output as 12345

share|improve this answer
    
@ Ajay kumar The value i'm passing is an API value . The data in the controller i'm passing consists of values in it. – anil chean 15 mins ago

First convert the response data into string and then parse it into integer this might be help try once

 paymentService.NewEReceipt().success(function(data){
                    console.log(data)
                    $scope.IssueReceipt.formId = parseInt(data.toString());
                    console.log($scope.IssueReceipt.formId);
                 })
share|improve this answer
    
still i'm getting an NAN value – anil chean 43 mins ago
    
data is already of type string. You dont want to convert that – Jaganathan Bantheswaran 39 mins ago
    
Try json.parse and then convert to int – Sujithrao 35 mins ago
    
@ Sujithrao is this the method you are telling paymentService.NewEReceipt().success(function(data){ console.log(data) $scope.IssueReceipt.formId = data; return JSON.parse(data); console.log(JSON.parse(data)); }) – anil chean 29 mins ago
    
@Sujithrao the API method that i'm using is an Post method . – anil chean 3 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.