设想
我有一个使用 ng-repeat 的转发器,它绑定到一组动态/运行时计算值。(即,我的数组包含像 Brokerage 这样的字段: ($scope.BuyValue() + $scope.SellValue())* 0.5/100 )。
问题
当我更改具有 ng-model 的输入框中的值时,转发器内的字段 Broker.Brokerage 不会被刷新。但我能够正确更新/绑定中继器之外的字段。
代码
<body ng-controller="BrokerController">
<div>
<div>
Buy Price <input type="textbox" ng-model="BuyPrice"/><br/>
Sell Price <input type="textbox" ng-model="SellPrice"/><br/>
Quantity <input type="textbox" ng-model="Quantity"/><br/>
Buy Value {{ BuyValue() }} <br/>
Sell Value {{ SellValue() }} <br/>
Profit {{ Profit() }} <br/><br/><br/>
<h4>Brokers</h4>
<div ng-repeat='Broker in Brokers'>
Broker Name : <b>{{Broker.BrokerName}}</b>
Brokerage Amount : <i>{{Broker.Brokerage}}</i> ({{Broker.BrokerageDetail}})
<br/>
</div>
</div>
</div>
<script id="angularjs" src="js/lib/angular/angular.js"></script>
<script>
var App = angular.module('ChooseYourBroker', []);
App.controller("BrokerController", ['$scope', function ($scope) {
$scope.BuyPrice = 10;
$scope.SellPrice = 20;
$scope.Quantity = 100;
$scope.BuyValue = function(){ return ( $scope.BuyPrice * $scope.Quantity )};
$scope.SellValue = function(){ return ( $scope.SellPrice * $scope.Quantity )};
$scope.Profit = function(){ return ( $scope.SellValue() - $scope.BuyValue() )};
$scope.Brokers = [
{
BrokerName: 'Broker one',
BrokerageDetail :'0.5% value of Sell Value',
Brokerage: $scope.SellValue() * 0.5/100
},
{
BrokerName: 'Broker two',
BrokerageDetail :'2% value of Sell Value',
Brokerage: $scope.SellValue() * 2/100
},
{
BrokerName: 'Broker three',
BrokerageDetail :'1% value of Sell Value',
Brokerage: $scope.SellValue() * 1/100
},
{
BrokerName: 'Broker Four',
BrokerageDetail :'0.5 % value of Buy Value and Sell Value',
Brokerage: ($scope.BuyValue() + $scope.SellValue())* 0.5/100
}];
}]);
</script>