我面临将字符串值转换为数字的问题,当我传入我的输入字段时。我使用了parseInt,而是获得了NAN值。如何在Angularjs中使用parseInt将字符串值转换为数字?
这里是我的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>
而且我的脚本网页,我传递值:
$scope.getRandomForm = function() {
paymentService.NewEReceipt().success(function(data){
console.log(data)
$scope.IssueReceipt.formId = parseInt(data);
console.log(parseInt(data));
})
}
$scope.getRandomForm()
让我告诉ü控制台我得到:
如果我使用parseInt,我得到NAN作为值而不是数。 请任何人都可以帮助我吗?
@Jaganathan Bantheswaran我传递的价值是一个API值。我传递的控制器中的数据由其中的值组成。 –
@Jaganathan Bantheswaran我得到的价值。但是,它是以字符串的形式出现的,但是我需要它的数量。 –
我知道'data'是字符串。我告诉你修剪字符串“data”的原因是为了避免字符串中有空格。尝试像这样''parseInt(data.trim())' – Thaadikkaaran