angular.isNumber的问题

时间:2016-10-14 08:33:01

标签: html angularjs

任何人都可以告诉我为什么我会遇到 angular.isNumber(数字(" 1s"))的问题

我得到的输出是:



var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.x1 = "1s";
    $scope.x2 = angular.isNumber(Number($scope.x1));
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

来自AngularJS doc

  

确定引用是否为数字。

     

这包括&#34;特殊&#34;数字NaN,+ Infinity和-Infinity。

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.x1 = "1s";
  $scope.number = Number($scope.x1); // number = NaN
  $scope.x2 = angular.isNumber(Number($scope.x1));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myCtrl">
    <p>x1 = {{ x1 }}</p>
    <p>Transform to number = {{ number }}</p>
    <p>isNumber = {{ x2 }}</p>
  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.x1 = "1s";
    $scope.x2 = angular.isNumber($scope.x1);
});

我认为这会奏效。

答案 2 :(得分:0)

我同意weedoze

您需要将代码替换为:

return angular.isNumber(Number($scope.txt).toString() == $scope.txt ? Number($scope.txt) : $scope.txt);

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
    $scope.x1 = "1s";
        $scope.x2 = angular.isNumber(Number($scope.x1).toString() == $scope.x1 ? Number($scope.x1) : $scope.x1);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>