I am in the process of learning how to make custom directives in AngularJS.
I have this simple program that works well and I would like to refactor it into a custom directive. I would like the directive element to be the div that is a child of the body tag. I am having trouble imagining how the slider values would be passed up and down the DOM from the input tags to the div and the other way around.
Code:
<html ng-app="DualSlidersApp">
<head>
<meta charset="UTF-8">
<title>DualSlidersApp</title>
<style>
.indented { margin-left: 61px; }
</style>
<script src="js/angular.js"></script>
<script>
'use strict';
var app = angular.module('DualSlidersApp', []);
app.controller('DualSlidersController', ['$scope', function($scope) {
$scope.firstRangeSliderValue = 10;
$scope.secondRangeSliderValue = 20;
$scope.reconcileSliders = function reconcileSliders(drivenByWhichSlider) {
if ($scope.firstRangeSliderValue > $scope.secondRangeSliderValue) {
if (drivenByWhichSlider === 'drivenByTheFirstSlider') {
$scope.secondRangeSliderValue = $scope.firstRangeSliderValue;
} else {
$scope.firstRangeSliderValue = $scope.secondRangeSliderValue;
}
}
}
}]);
</script>
</head>
<body ng-controller="DualSlidersController">
<div>
<table>
<tr>
<td>
<input type="range" min="10" max="30" step="2"
ng-model="firstRangeSliderValue"
ng-change="reconcileSliders('drivenByTheFirstSlider');">
</td>
<td>{{ firstRangeSliderValue }}</td>
</tr>
<tr>
<td>
<input type="range" min="20" max="40" step="2" class="indented"
ng-model="secondRangeSliderValue"
ng-change="reconcileSliders('drivenByTheSecondSlider');">
</td>
<td>{{ secondRangeSliderValue }}</td>
</tr>
</table>
</div>
</body>
</html>