我正在使用ionic和angularjs用于移动应用程序,我正在尝试使用pull来刷新,但是当刷新(从UI调用doRefresh)时,我收到错误
“未捕获的TypeError:无法读取未定义的属性'getFromServer'”
当控制器加载时,对getServiceList的调用正常工作
我无法理解为什么serviceListSerice返回一个未定义的对象。请帮忙
我的Controller.js
angular.module('starter.controllers', ['ionic', 'ui.bootstrap.datetimepicker'])
.controller('serviceListCtrl',function($scope,$rootScope,$state,serviceListService){
$scope.doRefresh=function(serviceListService,$scope) {
serviceListService.getFromServer($scope); //<-this is where i am getting the error
//$scope.$broadcast('scroll.refreshComplete');
};
serviceListService.getServiceList($scope); //<--- this is called successfully
//when controller is getting loaded.
})
我的services.js
angular.module('starter.services', [])
.factory('serviceListService',function() {
return {
getServiceList : function($scope) {
var jsonStr='({"data":[{"id":"1","CircuitID":"130101002","Customer No.":"4160891","CustomerName":"ZZZZ Customer","Last Order":"130101002","ServiceType":"Some String","StartDate":"41878.4125"},'+
'{"id":"2","CircuitID":"140501837","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140501837","ServiceType":"SOME STRING","StartDate":"41859.4083333333"},'+
'{"id":"3","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"4","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"5","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"6","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"7","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"8","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"9","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"10","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"11","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"12","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"13","CircuitID":"140502362","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"}'+
']})';
console.log(jsonStr);
var datas= eval(jsonStr);
$scope.services=datas.data;
},
loadMoreServices : function($scope) {
var updateList='({"data":[{"id":"14","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"15","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"16","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"17","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"18","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"19","CircuitID":"140502362","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"}'+
']})';
$scope.res.push(eval(jsonStr));
$scope.$broadcast('scroll.infiniteScrollComplete');
},
getFromServer:function($scope) {
var jsonStr='({"data":[{"id":"14","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"15","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"16","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"17","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"18","CircuitID":"140502361","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"},'+
'{"id":"19","CircuitID":"140502362","Customer No.":"A0156272","CustomerName":"SOME Company","Last Order":"140502361","ServiceType":"SOME STRING","StartDate":"41858.9583333333"}'+
']})';
$scope.res= eval(jsonStr);
$scope.$broadcast('scroll.refreshComplete');
},
clearSearch : function($scope) {
$scope.search = '';
}
}
})
答案 0 :(得分:1)
为什么要在$ scope函数中传递serviceListService?你不需要它,因为它是在整个控制器中声明的。试试这个:
$scope.doRefresh = function($scope) {
serviceListService.getFromServer($scope); //<-this is where i am getting the error
//$scope.$broadcast('scroll.refreshComplete');
};
答案 1 :(得分:1)
您不需要再次注入服务serviceListService
。更改代码如下并尝试!
$scope.doRefresh=function($scope) {
serviceListService.getFromServer($scope); //<-this is where i am getting the error
//$scope.$broadcast('scroll.refreshComplete');
};
答案 2 :(得分:1)
serviceListService当passong到scope函数时会覆盖工厂引用,现在是doRefresh函数的局部变量,显然不会有它们的工厂方法,不要传递它,因为你不需要
$scope.doRefresh=function($scope) {
serviceListService.getFromServer($scope); //<-this is where i am getting the error
//$scope.$broadcast('scroll.refreshComplete');
};
答案 3 :(得分:1)
$scope.doRefresh=function($scope) {
serviceListService.getFromServer($scope);
//$scope.$broadcast('scroll.refreshComplete');
};
您不需要将依赖项传递给控制器内部的内部函数,工厂serviceListService
可以在整个控制器中访问