When applying a filter on the controller to retrieve data from a web API, everything works correctly. However, I am trying to implement a function that will be called from the controller when the filter searches for records and finds no data.
.controller('statement_ctrl',function($scope,$http,$ionicLoading,$ionicPopup,$cordovaToast,$location,$ionicModal,$filter){
$scope.account_number=localStorage.getItem("account_number");
///alert if connection fails
$scope.connect = function() {
var alertPopup = $ionicPopup.alert({
title: 'Error',
template: '<p align="center">Problem Contacting Server</p>',
});
};
$scope.nobill = function() {
var alertPopup = $ionicPopup.alert({
title: 'Error',
template: '<p align="center">Billing not Found</p>',
});
};
$scope.state_request= function(){
$ionicLoading.show({template: '<p>Processing Request</p><ion-spinner></ion-spinner>'});
$http.post("http://myprojec/statement.php",{'id':$scope.account_number}).success
(function(data){
console.log(JSON.stringify(data));
$scope.record=data;
$scope.test='results';
//console.log(JSON.stringify(results));
{$ionicLoading.hide();}
})
$scope.from = $filter('date')($scope.sdate, "yyyy-MM-dd"+'T00:00:00')+'Z';
$scope.to = $filter('date')($scope.edate, "yyyy-MM-dd"+'T00:00:00')+'Z';
}
})
.filter('dateRange', function() {
return function(records, from, to, nobill) {
// return empty array if input not defined or not array
if(!records || !angular.isArray(records)){
return [];
}
var results = records.filter(function(record) {
// run console log tests here...before the return
return record.Date >= from && record.Date <= to;
console.log(test)
});
if(results.length==0){
nobill();
}else{
console.log('this is records in there')
}
console.log( 'Number of results:', results.length);
return results;
}
})
HTML
<form method="post" name="statement" id="statement">
<div class="list">
<label class="item item-input">
<span class="input-label">Search From</span>
<input type="date" ng-model="sdate" required="required">
</label>
<label class="item item-input">
<span class="input-label">Search To</span>
<input type="date" ng-model="edate" required="required">
</label>
</div>
</form>
</br>
<div align="center">
<p><button class="button button-positive" ng-disabled="statement.$invalid" ng-click="state_request()">
<i class="icon ion-android-search icon"></i> Search
</button></p>
</div>
If the length equals zero, I want to call the nobill function from the controller to the filter. Despite attempting to do so, an error stating that nobill is not a function occurred.