3

我想直接显示/更新计算答案,而不需要额外的按钮。还是我需要一个功能和按钮来应用更改?

<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>

控制器:

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) {

$scope.square = $scope.A_Number * $scope.A_Number; 
}); 
</script>
4

2 回答 2

4

作为一种方法并在square插值指令中使用该方法,以便对每个摘要进行评估。

标记

square: {{square()}} 

代码

$scope.square = function(){
    //made error free
    if(!isNaN($scope.A_Number))
      return $scope.A_Number * $scope.A_Number;
    return 0;
};
于 2016-01-30T14:15:40.587 回答
0

在 A_Number 上添加监视以实现相同的目的。JSFiddle 供参考 - https://jsfiddle.net/9a2xz61y/3/

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.square = 0;
  $scope.$watch('A_Number', function(newValue, oldValue) {
    {
      if (!isNaN(newValue)) {
        $scope.square = $scope.A_Number * $scope.A_Number;
      }
    }
  });
});
于 2016-01-30T14:28:55.793 回答