我想直接显示/更新计算答案而不需要额外的按钮。 或者我需要一个功能和按钮来应用更改吗?
<div ng-app="myApp" ng-controller="myCtrl">
A_Number: <input type="number" step="1" ng-model="A_Number"><br>
Display (A_Number): {{A_Number}}
<br>
square: {{square}}
</div>
Controller :
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.square = $scope.A_Number * $scope.A_Number;
});
</script>
最佳答案
将 square
作为方法并在插值指令中使用该方法,以便对每个摘要进行评估。
标记
square: {{square()}}
代码
$scope.square = function(){
//made error free
if(!isNaN($scope.A_Number))
return $scope.A_Number * $scope.A_Number;
return 0;
};
关于Angularjs 不更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102804/