2016-12-13 93 views
1

我創建一個web應用程序中,我計算了textbox一個$scope變量,但如果用戶把textbox空,或者如果用戶不上textbox輸入任何結果未來NaN這裏是我的代碼angularjs楠正在出現,如果文本框爲空

<input type="text" ng-model="amount1" ng-blur="amountchange(amount1)" /> 
{{total}} 

在我控制器

$scope.amountchange = function() { 
    $scope.total = parseInt($scope.amount1) + parseInt($scope.total); 
} 

我想擺脫掉NaN如果文本框爲空時被apearing

這裏是我更好地理解

CLICK HERE

+1

['提琴$ scope.total = parseInt($ scope.amount1)+ parseInt($ scope.total)|| 200;'](https://jsfiddle.net/tusharj/XNVj3/2252/) – Tushar

回答

4

創建使用

$scope.constant = 200;  //Define other variable to persist constants 
$scope.total = $scope.constant;  //Initialize total 
$scope.amountchange = function() {   
    var amount1 = parseInt($scope.amount1, 10) || 0; //parse amount if its invalid set it to zero 
    $scope.total = $scope.constant + amount1; //update total 
} 

DEMO

+0

但是如果我在200中添加500(它變成700),但是當我從文本框中刪除500時它仍然是700(而不是減少)它應該200如果我刪除500 –

+1

@MumbaiWadala,你想[這](http://jsfiddle.net/satpalsingh/mcyvrckk/1/) – Satpal

+0

是的,我想這 –

相關問題