I am a beginner in AngularJS and I am attempting to filter data based on temperature by subtracting the user input (x) from the currentTemp variable. However, I am unsure how to access the user input value in a custom filter. This approach may work for now, but I am also considering implementing a similar logic for dates in the future. Feedback on the feasibility of achieving this using a custom filter is appreciated, as well as any alternative logic suggestions.
http://plnkr.co/edit/Qzne7wfnSa2keRcvbUUV?p=preview
var app = angular.module('tempfilter', []);
app.controller('MainCtrl', function($scope) {
$scope.sensordata = [{name:'Rob',"Date": "2014-02-16T00:00:00.000Z","Temp":42},
{name:'Bob',"Date": "2012-02-16T20:27:11.507Z","Temp":50},
{name:'Tom',
"Date": "2012-02-16T20:27:11.507Z","Temp":60},
{name:'Sinclair',"Date": "2012-02-16T20:27:11.507Z","Temp":65}];
$scope.condEqual=function(){
var x = document.getElementById("myNumber").value;
document.getElementById("demo").innerHTML = x;
}
});
app.filter('tempo', function() {
return function( items, field ) {
var filtered = [];
var x=2;
var currentTemp=62;
angular.forEach(items, function(item) {
if (item[field]<= currentTemp){
filtered.push(item);
}
});
return filtered;
};
});
<!DOCTYPE html>
<html ng-app="tempfilter">
<head lang="en">
<meta charset="utf-8">
<title>DateFilter</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<input type="number" id="myNumber" value="2">
<button ng-click="condEqual()">Enter</button> </br>
<p id="demo"></p>
Filtered list:
<ul>
<li ng-repeat="s in sensordata | tempo:'Temp'">{{s.Date|date:'MM/dd/yyyy'}}
{{s.name}}
{{s.Temp}}
</li>
</ul>
Full List:
<ul>
<li ng-repeat="s in sensordata ">{{s.Date|date:'MM/dd/yyyy'}}
{{s.name}}
{{s.Temp}}
</li>
</ul>
</body>
</html>
(The main objective here is to filter data based on date, where the user can input a value (e.g., 3) and have all data displayed from three days ahead of the current date. For example, if today's date is 2/19/2015 and the user inputs 3, the filter should show data from the last three days. Currently experimenting with temperature filtering, but plan to apply the same logic for dates in the future).