Javascript 将函数传递到过滤器angularjs
我在一个控制器上应用了一个过滤器,它从一个正常工作的web API中提取数据。我试图实现的一件事是,当过滤器搜索记录时,没有发现任何数据,应该从控制器调用一个函数Javascript 将函数传递到过滤器angularjs,javascript,angularjs,Javascript,Angularjs,我在一个控制器上应用了一个过滤器,它从一个正常工作的web API中提取数据。我试图实现的一件事是,当过滤器搜索记录时,没有发现任何数据,应该从控制器调用一个函数 .controller('statement_ctrl',function($scope,$http,$ionicLoading,$ionicPopup,$cordovaToast,$location,$ionicModal,$filter){ $scope.account_number=localStorag
.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';
}
})
.controller('statement\u ctrl',function($scope、$http、$ionicLoading、$ionicPopup、$Cordovatose、$location、$IonicModel、$filter){
$scope.account\u number=localStorage.getItem(“account\u number”);
///连接失败时发出警报
$scope.connect=function(){
var alertPopup=$ionicPopup.alert({
标题:“错误”,
模板:“联系服务器时出现问题”,
});
};
$scope.nobill=函数(){
var alertPopup=$ionicPopup.alert({
标题:“错误”,
模板:“
未找到账单”
,
});
};
$scope.state_request=函数(){
$ionicLoading.show({模板:'处理请求'});
$http.post(“http://myprojec/statement.php“,{'id':$scope.account_number})。成功
(功能(数据){
log(JSON.stringify(data));
$scope.record=数据;
$scope.test='results';
//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(){
返回函数(记录、发件人、收件人、nobill){
//如果输入未定义或不是数组,则返回空数组
如果(!records | |!angular.isArray(records)){
返回[];
}
var结果=记录。过滤器(函数(记录){
//在返回之前…在此运行控制台日志测试
返回记录.Date>=开始日期(&record.Date)
这是一个实现服务、将其注入控制器并从控制器调用服务方法的基本框架,该控制器将回调传递给$scope.nobill()。我还没有测试它,因此您可能需要对其进行一些调整,但它应该可以工作,并且应该能让您大致了解
恐怕这是我能想到的最简单的版本了。希望这能有所帮助。我想你需要一个能为你提供日期范围功能的服务。这样你就可以将回调传递给控制器。我该怎么做?
.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;
}
})
<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>
var APP = angular.module('MYAPP');
APP
.factory('myDateSvc', [function() { // this is one way to define a service
return {
dateRange: function(records, from, to, nobill) {
... put your code here ...
nobill();
}
}
}])
.controller('statement_ctrl', ['$scope', 'myDataSvc', function($scope, myDataSvc) {
... you code ...
$scope.nobill = function() { ... };
myDataSvc.dateRange(recordsVal, fromVal, toVal, $scope.nobill);
... more of you code ...
}]);