我试图基于输入获得折现值,并且希望它是动态的,所以我尝试做ng-change="detail.total = detail.total - ((detail.discount/100)*detail.total)"
,但是它不起作用,因为它将引用新更改的detail.total
,实际上应该引用的是旧的,不变的值。
这是一个代码段:
<ui-select ng-model="detail.item" theme="bootstrap" ng-change="detail.price = detail.item.price">
<ui-select-match placeholder="Select item" allow-clear>{$$select.selected.name$}</ui-select-match>
<ui-select-choices repeat="item in main.items | propsFilter: {name: $select.search, price: $select.search} | limitTo: 100">
<div ng-bind-html="item.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<input type="text" ng-model="detail.qty" ng-change="detail.total = (detail.qty * detail.price)">
<input type="text" ng-model="detail.price" ng-change="detail.total = (detail.qty * detail.price)">
<input type="text" ng-model="detail.discount" ng-change="detail.total = detail.total - ((detail.discount/100)*detail.total)>
<input readonly type="text" ng-model="detail.total">
我在这里做什么错了?
编辑:试图在我的控制器内完成-
this.new_total = function(total, discount){
var total_c = angular.copy(total);
var discount_c = angular.copy(discount);
discount_c = discount_c/100;
var final = total_c - (discount_c*total_c);
return final;
}
然后我通过执行此操作将其称为:
<td><input type="text" class="form-control" ng-model="detail.discount" ng-change="detail.total = main.new_total(detail.total, detail.discount);"></td>
它仍然有相同的问题
答案 0 :(得分:0)
如注释中所建议,每次发生变化时,使用控制器中的函数更新总数。
请参见以下示例:
#id
var app = angular.module("App", []);
app.controller("main", ['$scope', function ($scope) {
$scope.detail = {
'item': null,
'price': null,
'qty': null,
'discount': null,
'total': null
};
$scope.updateTotal = function () {
var totalBeforeDiscount = ($scope.detail.qty * $scope.detail.price);
$scope.detail.total = totalBeforeDiscount - (totalBeforeDiscount * ($scope.detail.discount / 100));
}
}]);