angular js如何删除默认值0

时间:2018-04-02 11:36:47

标签: javascript angularjs html5

enter image description here          

使用AngularJS

的两个数字的总和               

使用双重否定

        第一个数字:         第二个数字:         总:
    

2 个答案:

答案 0 :(得分:0)

默认情况下,AngularJS将未初始化的模型视为null。因此,在您的情况下,ab都是null

这就是原因:

null - -null // {{ a -- b }} is {{ a - -b }} actually

将导致0。

答案 1 :(得分:0)

希望这将涵盖您的要求。谢谢。

var app = angular.module('demo', []);
app.controller('mainController', ['$scope',
  function($scope) {
 $scope.testfunction = function(value) {
    return value>0 ?value:'';
  }  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<html ng-app="demo">
<div ng-controller="mainController as mainControl">
<img src="https://angularjs.org/img/AngularJS-large.png" height="50px" weight="50px"/>
<h3>Sum of two Numbers using AngularJS</h3>
<div>
     <h3>Using Double Negation</h3>
    First Number: <input type="text" ng-model="a" />
    Second Number: <input type="text" ng-model="b" />
    Sum:<input type="number" value='{{testfunction(a--b)}}' >       
</div></div>
</html>