我已經做了一個文本框,我想顯示在div內的數字(輸入在textfiled中)的計算值。我的html:計算範圍變量的角度js的輸入值
<input ng-model="inp" type="text"/><br/>
<div>{{equateInp}}</div>
angularjs:
$scope.inp = "";
$scope.equateInp = parseInt($scope.inp);
,但它無法正常工作。請幫助
我已經做了一個文本框,我想顯示在div內的數字(輸入在textfiled中)的計算值。我的html:計算範圍變量的角度js的輸入值
<input ng-model="inp" type="text"/><br/>
<div>{{equateInp}}</div>
angularjs:
$scope.inp = "";
$scope.equateInp = parseInt($scope.inp);
,但它無法正常工作。請幫助
試試這個
<input type = "number" ng-model = "inpNumber">
<div>{{inpNumber}}</div>
這裏是舉例,
<input ng-model="inp" type="text"/><br/>
<div>{{calculate(inp)}}</div>
$scope.equateInp = function(inp) {
// do your calculation here
//eg:
c = parseInt(inp) + 10
return c
}
這裏是的jsfiddle Demo,
的整數是一種原始的JavaScript類型,而2-way綁定在原始類型上不起作用。因此,您需要爲對象屬性指定雙向綁定模型或使用ng-change
。由於yourcase你需要在你的模型(parseInt
)執行一些操作,您可以使用通過如下NG-變化的第二個選項:
JS:
$scope.user = {
inp: 0
};
$scope.onChange = function() {
$scope.equateInp = parseInt($scope.user.inp);
};
HTML:
<input ng-model="user.inp"
ng-change='onChange()'
type="text"/>
<br/>
<div>{{equateInp}}</div>
的工作JSBin這可以在這裏找到:https://jsbin.com/nuxaqak/edit?html,js,console,output
編輯:更新JSBin處理表達式 https://jsbin.com/nuxaqak/edit?html,js,output
soryy先生,但我沒有得到10值,當我輸入5 + 5在輸入 – user3450590
但你的問題沒有提到你會輸入一個表達式。它只是說'數字的計算值'。請編輯您的問題,提出確切的期望 –
好像對我說,你想觀看在INP VAR計算值和outputing結果變化的評價。 有多種方法可以完成此操作。使用
$表:
HTML:
<input ng-model="inp" type="text"/><br/>
<div>{{ equateInp }}</div>
angularjs:
$scope.inp = "";
$scope.$watch('inc', function() {
$scope.equateInp = parseInt($scope.inp);
})
當INC變量改變這個基本手錶。
使用的計算方法:
HTML:
<input ng-model="inp" type="text"/><br/>
<div>{{ calculate(inp) }}</div>
angularjs:
$scope.inp = "";
$scope.calculate = function(value) {
return ($scope.inp * 5);
}
此每次變量改變
01時間用完的計算方法
您是否正確配置了控制器? – chresse
你的一些更多的代碼,可以幫助我們來幫助你 – manish
請附上您的控制器 –