我正在寫一個簡單的控制器,它的計算很少。這是一個真實項目的更多信息的反映。 問題是,當作爲表達式放置在html上時,每次更改都會重新計算結果,但是,當我在$ scope中作爲變量進行計算時,結果不會更新。請參閱標記中的評論。2種方式的數據綁定不起作用
任何想法我在這裏失蹤?
標記
<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]
}]);
http://codepen.io/Tallyb/pen/rVdebm
結果在哪裏綁定結果?缺少ng-model =「結果」? –
兩個選項可以添加更改偵聽器或http://codepen.io/anon/pen/dompRM – mido