如何将计算出的ng-model值获取到另一个ng-model中
how to fetch calculated ng-model value into an another ng-model
我有3个ng模型,我需要使用angularjs获取前两个计算数据到第三个
我试过这样做,但不工作
HTMLvar urlt = 'http://localhost/tripsheets';
app.factory('tripsheetFactory', function ($http) {
return {
getTripsheets: function () {
return $http.get(urlt + '/all');
},
addTripsheet: function (tripsheet) {
return $http.post(urlt, tripsheet );
},
deleteTripsheet: function (tripsheet) {
return $http.delete(urlt + '?id=' + tripsheet.id);
},
updateTripsheet: function (tripsheet) {
return $http.put(urlt + '?id=' + tripsheet.id, tripsheet);
}
};
});
app.controller('TripsheetCtrl', function ($scope, tripsheetFactory) {
$scope.tripsheets = [];
$scope.getTotalCalc = function () {
return $scope.tripsheets.cash*$scope.tripsheets.tax;
};
tripsheetFactory.getTripsheets().then(function(data){
$scope.tripsheets = data.data;
$scope.tripsheet = {
tripsheet_num: $scope.tripsheets.length +1,
cash:null,
tax:null,
total: $scope.getTotalCalc()
};
});
});
如果您还没有考虑过,可以在tripsheet对象上使用watch并计算watch中的值
$scope.$watch('tripsheets', function(val){
$scope.tripsheets.total = $scope.tripsheets.cash*$scope.tripsheets.tax;
}, true);
示例演示:http://plnkr.co/edit/Ml6eLM4vCYz3tPaL8j3n?p=preview
你可以设置一个手表的总数…
$scope.$watch(function() {
return $scope.tripsheets.cash * $scope.tripsheets.tax;
}, function(value) {
$scope.tripsheets.total = value;
});
小提琴
或者使用Object.defineProperty()(需要ES5+)…
Object.defineProperty($scope.tripsheets, 'total', {
get: function(){
return $scope.tripsheets.cash * $scope.tripsheets.tax;
}
});
小提琴
相关文章:
- 从 Javascript 中设置 ng-model name
- Toggle Switch在包含ng model和ng repeat之后不进行切换
- 在Angularjs中,我如何使用ng repeat、ng model和ng click来动态更改内容
- 使用 ng 模型获取数据时遇到问题
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- angularJS从ng-repeat获取类名
- ng-model 绑定到选择标签生成的空默认值
- 对象不能在angularjs ng-model中访问
- 为什么 HTML 中的 Value 属性在与 AngularJS 中的 ng-model 一起使用时不起作用
- 当输入ng-model是ngRepeat变量时,如何从输入数据中获取
- NG-model 未定义多种方式无法从文本框获取文本
- AngularJs 指令在更改时获取单选按钮 ng-model 值
- 使用getterSetter从ng-model中获取无限摘要循环
- AngularJS -无法从ng-model获取值到控制器
- Angular的ng-click无法获取ng-model的值
- 通过ng-repeat, ng-model和复选框获取数组中的位置
- 从控制器获取未定义的ng-model
- 从模板中获取ng-model值
- 如何将计算出的ng-model值获取到另一个ng-model中
- Angular JS:在ng-change上获取ng-model