search

Home  >  Q&A  >  body text

Front-end - string conversion problem when using angularjs

I want to use angular to make an automatic adder, the code is as follows

    <input type="text" ng-model="add1">
    <input type="text" ng-model="add2">
    <p>total={{add1+add2}}</p>

This code simply connects strings. I tried to use parseInt(add1) parseInt(add2) but it didn’t work. I don’t know if Angular has any method to convert characters into numerical values.

巴扎黑巴扎黑2779 days ago523

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-15 16:53:59

     <p>total={{add1 * 1 + add2 * 1}}</p>
    

    reply
    0
  • 阿神

    阿神2017-05-15 16:53:59

    You can add the corresponding function in $scope

    $scope.parseInt = parseInt;
    

    Then you can use it

    parseInt(add1) + parseInt(add2)
    

    reply
    0
  • Cancelreply