I am using Angular to perform a real-time database search. I have bound the variable $scope.city in my HTML like this:
<div class="col-lg-8">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">City@</span>
<input type="text" ng-model="city" class="form-control" aria-describedby="basic-addon1">
</div>
</div>
Here is the controller code:
app.controller('forecastController', ['$scope','varService','$http', function($scope,varService,$http){
$scope.city = varService.city;
$scope.days = 2;
$scope.$watchGroup(['days', 'city'], function() {
$http.get('http://api.openweathermap.org/data/2.5/forecast?q='+$scope.city+'&mode=json&appid=e652a2c384655ea24f5b12d2fb84c60f&cnt='+$scope.days+'&units=metric')
.then(function(data) { $scope.forecast = data; });
});
}]);
Currently, the $http.get() method retrieves data every time the $scope.city changes, which is consuming too many resources. I am looking for a solution where it only calls $http.get every 3-4 seconds after the user inputs something. Using $timeout did not solve the issue. Can anyone help me with this? Thank you.