1

i am trying to convert json object to integer in ng-view:

json string:

$scope.json_arr = [{
                    'id': '1',
                    'name': 'abc'
                },
                {
                    'id': '2',
                    'name': 'xyz'
                }];

in view:

<ul ng-repeat="vals in json_arr">
    <li><input type="number" ng-model="vals.id" /></li>
    <li><input type="text" ng-model="vals.name" /></li>
</ul>

when i see the ouput, the number field is coming blank as the value of the id object is string.

How do i convert string 'id':'1' to integer??

Demo Plunker

3 Answers 3

4

Use ng-init, this will run whenever a new element in the array appears for ng-repeat, if you use it like this:

<ul ng-repeat="vals in json_arr" ng-init="parseId(vals)">
    <li><input type="number" ng-model="vals.id" /></li>
    <li><input type="text" ng-model="vals.name" /></li>
</ul>

Then all that's left is to write the parseId function referenced

  $scope.parseId = function(val){
    val.id = parseInt(val.id);
  }

Plunkr

Sign up to request clarification or add additional context in comments.

Comments

1

try it

Html side

<ul ng-repeat="vals in json_arr">
    <li> <input type="number" value="{{convertToInt(vals.id)}}"/></li>
    <li><input type="text" ng-model="vals.name" /> </li>
</ul>

Js Side

$scope.convertToInt= function (value) {
            return parseInt(value);
        };

It's working for me.

Working Source code here

7 Comments

how do i use value , it should be ng-model right?
But you can define the ng-model in this other type of way {{property name}} .. It is same as you defined ng-model="vals.id" .
can you please create OR update my plunker so that i can get a clear picture because i've tried your code, but didn't worked for me :(
I have posted new plunker link . @Jay Please take a look plnkr.co/edit/S9coDaJyyjKhKbzQhP42?p=preview
yeah thats correct but when i write ng-model="vals.id" its not working, and without ng-model how do i bind textbox value current scope??
|
0

if you are using angular form to store one or more variable values and pass a request for api post or put method, example:

component.ts file

    `loginForm: FormGroup;
     mockService: ServiceName;
     
         this.loginForm = this.formBuilder.group({
          Id: [''],
          empID: [''],
          roomID: ['']      
        });
           this.loginForm.get('Id').setValue(parseInt(this.Id));
    this.loginForm.get('empID').setValue(parseInt(this.empID));      
     this.loginForm.get('roomID').setValue(parseInt(this.roomID));
    
let request = JSON.stringify(this.loginForm.value);

this.mockloginService.loginValuesCheckmethod(request).subscribe((response) => {
      if (response.isSuccess && response.responseCode === '200') {}

This will pass payload value as int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.