service.jsTại sao .then không phải là một chức năng?
.factory('EventService', function($http, $cordovaSQLite) {
return {
//some code here..
populateData: function(data) {
var items = [];
for (i = 0; i < data.length; i++) {
items.push(data[i]);
}
return items;
}
}
})
controller.js
.controller('NearCtrl', function($scope, $http, $cordovaSQLite, EventService) {
EventService.getDataFromDB().then(function(result) {
if (result.length > 0) {
EventService.populateData(result).then(function(items) {
$scope.items = items;
})
} else {
EventService.getDataFromApi().then(function() {
EventService.getDataFromDB().then(function(result) {
EventService.populateData(result).then(function(items) {
$scope.items = items;
})
})
})
}
});
})
Khi tôi đang cố gắng để chạy mã này, tôi nhận được "Lỗi Loại: EventService.populateData (...). thì không phải là chức năng
Tôi đang làm gì sai? Cảm ơn.
'populateData' không trả lại một lời hứa, bạn không nhận được' then' gắn liền với tất cả mọi thứ theo mặc định. nó là một phần của đối tượng hứa hẹn. Bạn chỉ cần thực hiện '$ scope.items = EventService.populateData (kết quả)' – PSL