我正在实现此处的angularJS滑块:
https://github.com/venturocket/angular-slider
我让滑块正常工作,但现在需要从它们中提取值来执行一些简单的计算。
HTML:
<p>I'd like to borrow: {{calculate.amount}}</p>
<slider floor="100" ceiling="1000" precision="0" ng-model="calculate.amount" translate="currencyFormatting"></slider>
<p>and pay over a period of {{calculate.length}} months</p>
<slider floor="12" ceiling="120 months" precision="0" ng-model="calculate.length" translate="monthFormatting"></slider>
在我的控制器中,我目前有以下代码,包括我尝试获取滑块的当前值。
angular.module('myApp').controller('calculatorCtrl', ['$scope', '$http', function ($scope, $http) {
console.log('');
console.log($scope);
$scope.interestRate = 8.9;
$scope.calculate = {};
// $scope.amount = $scope.calculate.amount;
$scope.costPerMonth = ($scope.calculate.amount / $scope.calculate.length);
// $scope.loanAmount = parseInt($scope.calculate.amount, 10);
console.log($scope.loanAmount);
$scope.currencyFormatting = function (value) {
console.log(value);
console.log('currency formatting');
return value.toString() + " £";
}
$scope.monthFormatting = function(value) {
return '$' + value;
}
}]);
我怀疑,这纯粹是猜测,我需要某种回调才能访问滑块值?
编辑:
我知道这在视图中起作用:
<p>At a cost of {{calculate.amount / calculate.length}} per month</p>
但是我希望能够在控制器中操纵这些值!
答案 0 :(得分:2)
您可以使用此处的通信控件:https://github.com/angular-ui 将滑块绑定到模型,然后像访问任何其他控件一样访问这些值,因为指令会为您处理绑定。