Angular将NaN值显示为输入

时间:2016-07-16 22:30:31

标签: angularjs input angular-ngmodel

我发现默认情况下,带有10 rem you can set up n in advance here 20 n = 23 30 gosub 50 40 rem n is now 24 50 rem start of subroutine; this line is not needed, it's just here for clarity 60 n=n+1 70 print n 80 return 90 rem now you can call the subroutine on line 50 and it'll return at line 80 指令的Angular不会在输入中显示ng-model值:

NaN

因此,如果<input type="number" class="form-control" id="calcmouldSugNOfCavitiesCost" disabled step=0.01 ng-model="$parent.mouldTAB.sugNOfCavitiesCost"> 等于$parent.mouldTAB.sugNOfCavitiesCost,则输入为空。

我希望展示NaN而不是此行为。

有可能吗?

2 个答案:

答案 0 :(得分:2)

要达到预期效果,请使用以下选项

选项1:
1,将输入类型更改为文本
2.检查输入类型,如果字符串设置输入的默认值为&#39; NaN&#39;

HTML:

<html>

<body>
<div ng-app="myApp" ng-controller="myCtrl">

<input type="text" id="calcmouldSugNOfCavitiesCost" disabled

     ng-model="sugNOfCavitiesCost">

</div>

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

JS:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.sugNOfCavitiesCost ='123';
  if(typeof($scope.sugNOfCavitiesCost)=='string'){

    $scope.sugNOfCavitiesCost ='NaN';

  }

});

Codepen- http://codepen.io/nagasai/pen/jAYoxv

选项2:

要使用现有代码输入字段作为数字来实现预期结果,请再添加一个文本字段以显示&#39; NaN&#39;包含字符串值并使用ng-show和ng-hide根据值

显示输入字段

HTML:

  <div ng-app="myApp" ng-controller="myCtrl">
    <input type="number" id="calcmouldSugNOfCavitiesCost" disabled ng-model="sugNOfCavitiesCost" ng-hide="toggle">
    <input type="text" disabled value="NaN" ng-show="toggle">
  </div>

JS:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.sugNOfCavitiesCost ='123';
  if(typeof($scope.sugNOfCavitiesCost)=='string'){
    console.log($scope);
    $scope.toggle =true;
    }

});

Codepen- http://codepen.io/nagasai/pen/xOpNrw

答案 1 :(得分:0)

好吧,因为它是<input type="number">,所以它不能包含NaN作为值。

输入类型编号只能包含numbers,字母“e”或符号(+ / - )。

另一方面,您可以使用text type,这样您就可以检查控制器中的值是否为空,然后您可以将NaN归属于它,如下所示:

(function() {
  "use strict";
  angular.module('app', [])
    .controller('mainCtrl', function($scope) {
      $scope.test = '';
      
      if (!$scope.test) {
        $scope.test = "NaN";
      }
    });
})();
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl">
  <input type="text" class="form-control" id="calcmouldSugNOfCavitiesCost" ng-model="test" disabled>
</body>

</html>

我希望它有所帮助。