我正在编写一个简单的控制器,它做的计算很少。这是具有更高级信息的真实项目的反映。问题是结果,当作为表达式放置在 html 上时,每次更改都会重新计算结果,但是,当我在 $scope 中作为变量进行计算时,它不会更新。请参阅标记中的评论。
知道我在这里缺少什么吗?
标记
<body ng-app="myApp">
<div ng-controller="mainController">
<h3> Numbers </h3>
<label> a </label>
<input type="number" ng-model="numbers.a"/>
<label> b </label>
<input type="number" ng-model="numbers.b">
<br>
<br>
<span> Result : {{result}} {{numbers.a*numbers.b}} </span> // the first one does not update, but the second does.
<h3> Nums </h3>
<label> a </label>
<input type="number" ng-model="a1">
<label> b</label>
<input type="number" ng-model="b1">
<br>
Result2: {{result2}} {{a1+b1}}
<ul>
<li ng-repeat=" i in cool"> {{i}} </li>
</ul>
</div>
</body>
javascript:
angular.module('myApp',[])
.controller('mainController', ['$scope', function($scope) {
$scope.numbers = {a:11, b:10};
$scope.a1 = 5;
$scope.b1 = 7;
$scope.result = $scope.numbers.a*$scope.numbers.b;
$scope.result2 = $scope.a1 +$scope.b1 ;
$scope.cool = [$scope.result + $scope.result2,
$scope.result - $scope.result2]
}]);